the difference between os.mkdir() and os.makedirs()

20,962

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!

Share:
20,962
shuanglu
Author by

shuanglu

Updated on August 10, 2020

Comments

  • shuanglu
    shuanglu almost 4 years

    I use python 2.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? Thanks in advance