Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 425
shuanglu Avatar asked Jun 21 '16 15:06

shuanglu


People also ask

What does os mkdir mean?

mkdir() method in Python is used to create a directory named path with the specified numeric mode. This method raise FileExistsError if the directory to be created already exists.

What is the use of mkdir () and Mkdirs () methods in Python?

Creating a directory is a common operation in Python when you're working with files. The os. mkdir() method can be used to create a single directory, and the os. makedirs() method can be used to create multi-level directories.

What does os Makedirs do in Python?

makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os. makedirs() method will create them all.

How do you use mkdir in Python?

Python method mkdir() create a directory named path with numeric mode mode. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out.

What is the difference between mkdir() and makedirs()?

mkdir()can create a single sub-directory, and will throw an exception if intermediate directories that don't exist are specified. Either can be used to create a single 'leaf' directory (dirA): os.mkdir('dirA') os.makedirs('dirA') But makedirs must be used to create 'branches': os.makedirs('dirA/dirB')will work [the entire structure is created]

What is the use of OS makedirs in Python?

All functions in os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system. os.makedirs () method in Python is used to create a directory recursively.

How to create a directory using the OS module?

The os.mkdir () method can be used to create a single directory, and the os.makedirs () method can create multi-level directories. As we now know, the OS Module had its built-in os.mkdir () method for creating a directory within the Operating System.

Will makedirs throw exception if leaf exists?

os.makedirs(new_path, exist_ok=True) will not throw exception – keepscoding Aug 11, 2017 at 15:41 Add a comment | 28 (Can not comment, just add to NPE's answer.) In Python3, os.makedirshas a default parameter exist_ok=False. If you set it to True, then os.makedirswill notthrow any exception if the leaf exists.


1 Answers

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!

like image 55
mkarts Avatar answered Nov 15 '22 06:11

mkarts