Search code examples
pythondirectoryuser-inputsubdirectory

os.makedirs - I want to let user input the path they want to create folder in


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?


Solution

  • 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)