Reputation: 75
I use Python2.7 to create a Spider in PyCharm to get data from website.
In the first spider I create a Spider in the project folder and use os.mkdir('home/img/')
to create a folder to save data. There is no error.
In the second spider I create the spider with RedisQueue which is in the project folder and put the Spider.py into /usr/lib/python2.7. When I use os.mkdir('home/img/')
it reports the error
no such file or dir
and I change it to os.makedirs()
which works.
May I know why the 1st one doesn't meet error?
Upvotes: 7
Views: 23738
Reputation: 677
os.makedirs() : Recursive directory creation function. Like
os.mkdir(), but makes all intermediate-level directories needed to contain the leaf directory.
What this means is that you should not try to create nested directories with os.mkdir()
but use os.makedirs()
instead.
In your case, I am guessing that you want to create a directory under your home directory, in which case you would need something like os.mkdir("/home/img")
, which will fail if you do not have enough permissions.
You could try and do something like: os.chdir('/home')
and after that os.mkdir('img')
so you create home/img in steps! Good luck!
Upvotes: 16
Reputation: 9
Difference between os.mkdir(dirname) and os.mkdirs(dirname)
mkdir() will create only neaded directory. If some of parent directories was not existing, mkdir() will return false. mkdirs() will create the last directory with all missing parent directories.so mkdirs() is more handy.
Upvotes: -2