0

I want to create folder name 1 in folder abc. This is the code.

id=input("enter user id : ")

path = "/abc/" + str(id)

try:
    os.mkdir(path)
except OSError:
    print ("Creation of the directory %s failed" % path)
else:
    print ("Successfully created the directory %s " % path)

I enter 1 for create folder name 1 but it show error like this. How to fix it?

enter user id : 1
Creation of the directory /abc/1 failed
1
  • 3
    Apparently, an OSError exception is raised - look at the exception and see what message or error code comes with it. Commented Aug 24, 2020 at 4:50

2 Answers 2

2

Try using:

id = input("enter user id : ")
path = "abc/{}".format(id)
try:
    file = open(path)
except Exception:
    if not os.path.exists(path):
        os.makedirs(path)
Sign up to request clarification or add additional context in comments.

Comments

1

Just try:

if not os.path.isdir(path):
    os.mkdir(path)

This will create the folder if the folder will not exist.

and try to create the path of the file like this:

path = os.path.join('os.chdir()','abc','id)

using os.path.join is a better practice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.