Suraj
Suraj

Reputation: 21

Error while Creating directory using mkdir and loop in python

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

Upvotes: 0

Views: 1710

Answers (1)

edusanketdk
edusanketdk

Reputation: 602

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)

Upvotes: 1

Related Questions