0

My project is to create a script that can let user open multiple folder at once.

This is the part of the code that cause me problems.

try:
    os.makedirs(r'C:\Users\example\Documents\Sandbox\\' + filename[0])
    suscess_msg()
except OSError as e:
    if e.errno != errno.EEXIST:
        raise

I would like to open a folder in C:\Users\example\Documents\Sandbox\\, but I want to let user type the path that they want to open their folders in.

So, I tired to change it to this

folderpath = input("choose your path")

#some other codes

try:
    os.makedirs(rfolderpath + filename[0])
    suscess_msg()
except OSError as e:
    if e.errno != errno.EEXIST:
        raise

But this makes the r doesn't work, python think it is rfolderpath instead of r and folderpath separately and failed to create folders.

The usage of r is to change the text inside the '' to raw, without it, it can't be viewed as path by the system and the code won't work.

I tried some other way like these in order to separate them without losing its function.

os.makedirs(r folderpath + filename[0])
os.makedirs(r + folderpath + filename[0])
os.makedirs(r'folderpath' + filename[0])
os.makedirs(rinput("choose your path") + filename[0])

However, they all end up doesn't work.

Is there any way to seprate r and folderpath without losing it function and let the user input the path?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Is there a particular reason you're trying to use raw strings for everything? What do you mean by "it can't be viewed as path by the system and the code won't work"? – will-hedges Jun 18 '22 at 08:23
  • The `r` prefix is only applicable to a string literal, and all that does is to disable the interpretation of the backslash character at the syntax level. Values input by users are not code and thus not treated as syntax so you were chasing a phantom. It probably would saved yourself some time testing out printing out what `folderpath` actually is, before imagining random situations beyond a small, bite-sized problem. – metatoaster Jun 18 '22 at 08:45

1 Answers1

0

Try simple os.makedirs(folderpath + filename[0]) and it can work perfectly just try it, but if you want something that can convert normal string to Raw string so you can use python built-in function repr() like this rfolderpath = repr(folderpath)

Mehmaam
  • 573
  • 7
  • 22