0

I wanted to create directories of the definite names on python. I used jupyter and in the first case it worked perfectly well.

I am using a window system mkdir suraj It creates a directory named suraj.

Since this worked, I set up a loop to create 10 directories.

for i in range(1,10):
    x= "dir"+str(i)
    mkdir(x)

Error is :-

NameError Traceback (most recent call last) <ipython-input-5-9da2633c1170> in <module>
      1 for a in d:
----> 2     mkdir(a)

NameError: name 'mkdir' is not defined

Why mkdir is not defined in the name although it successfully created a director in the above command.

Image of Code with Error:

enter image description here

0

1 Answer 1

1

You are almost there!

Basically mkdir is a function within OS module. So, unless you did not import mkdir explicitly, it's gonna raise a NameError.

So, the right code would be-

from os import mkdir
for i in range(1,10):
    x = "dir"+str(i)
    mkdir(x)
Sign up to request clarification or add additional context in comments.

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.