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
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.
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.
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.
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.
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]
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With