- How can I create directories recursively? [duplicate]
- 5 Answers 5
- Creating and Deleting Directories with Python
- Required Python Modules
- Detecting the Current Working Directory
- Creating a Directory
- Creating a Directory with Subdirectories
- Creating a Temporary Directory
- Free eBook: Git Essentials
- Deleting a Directory
- Conclusion
- Links and References
- Acknowledgements
How can I create directories recursively? [duplicate]
Can I do it recursively or I have to create one directory after the other? The same thing for: chmod and chown can I do it recursively without assign permissions for each file/dir?
5 Answers 5
starting from python 3.2 you can do this:
import os path = '/home/dail/first/second/third' os.makedirs(path, exist_ok=True)
thanks to the exist_ok flag this will not even complain if the directory exists (depending on your needs. ).
starting from python 3.4 (which includes the pathlib module) you can do this:
from pathlib import Path path = Path('/home/dail/first/second/third') path.mkdir(parents=True)
starting from python 3.5 mkdir also has an exist_ok flag — setting it to True will raise no exception if the directory exists:
path.mkdir(parents=True, exist_ok=True)
Thanks for help. I always was using os.mkdir which didn’t make grandchildren and more nested children. os.makedirs just solved my problem
os.makedirs is what you need. For chmod or chown you’ll have to use os.walk and use it on every file/dir yourself.
import os import errno try: os.makedirs() except OSError as e: if errno.EEXIST != e.errno: raise
Welcome to Stack Overflow! Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
I would say this answer is the correct one for Python 2.x, since it handles errors correctly and doesn’t asks for file system for path twice (as with os.path.exists approach).
Here is my implementation for your reference:
def _mkdir_recursive(self, path): sub_path = os.path.dirname(path) if not os.path.exists(sub_path): self._mkdir_recursive(sub_path) if not os.path.exists(path): os.mkdir(path)
If the first folder doesn’t exist this function will result in stack overflow ( if not os.path.exists(sub_path) will always be true and call itself). But as long as the first dir exist this seems to be working.
I agree with Cat Plus Plus’s answer. However, if you know this will only be used on Unix-like OSes, you can use external calls to the shell commands mkdir , chmod , and chown . Make sure to pass extra flags to recursively affect directories:
>>> import subprocess >>> subprocess.check_output(['mkdir', '-p', 'first/second/third']) # Equivalent to running 'mkdir -p first/second/third' in a shell (which creates # parent directories if they do not yet exist). >>> subprocess.check_output(['chown', '-R', 'dail:users', 'first']) # Recursively change owner to 'dail' and group to 'users' for 'first' and all of # its subdirectories. >>> subprocess.check_output(['chmod', '-R', 'g+w', 'first']) # Add group write permissions to 'first' and all of its subdirectories.
EDIT I originally used commands , which was a bad choice since it is deprecated and vulnerable to injection attacks. (For example, if a user gave input to create a directory called first/;rm -rf —no-preserve-root /; , one could potentially delete all directories).
EDIT 2 If you are using Python less than 2.7, use check_call instead of check_output . See the subprocess documentation for details.
Creating and Deleting Directories with Python
This article continues with our series on interacting with the file system in Python. The previous articles dealt with reading and writing files. Interestingly, the file system is much more than a way to store/retrieve data to disk. There are also various other types of entries such as files, directories, sockets (for inter-process communication), named pipes, both soft and hard links, as well as special files (block devices). Reading and writing from and to them is done in a similar way as we saw in the previous articles.
This article focuses on the handling of directories. Other operating systems, like UNIX/Linux, instead use a different terminology, where an «entry» is named a «folder». Next, we will show you how to identify the current working directory, how to create both a persistent and a temporary, single directory as well as nested directory structures with subfolders, and how to remove a directory if no longer needed. Therefore, the two Python modules os and tempfile come into play.
Required Python Modules
Reading and writing files does not require loading an additional module, but accessing the file system functions (like handling directories) requires that we use a separate module. First, the os module has to be loaded. os is a Python module which belongs to the core part of the Python ecosystem. It is done using an import statement as follows:
The os module contains most of the methods we’ll need throughout this article. However, as you’ll see later on, if you want to something more advanced, like create a temporary file for storing data, then we’ll also be needing the tempfile module.
Detecting the Current Working Directory
Before we get in to creating/removing directories, let’s see how to perform some other basic directory operations, like detecting the current working directory using the method getcwd() . This method will return a string containing the path of your working directory. Listing 1 shows how to integrate this method in a Python script.
# import the os module import os # detect the current working directory and print it path = os.getcwd() print ("The current working directory is %s" % path)
The output should look something like this:
$ python3 cwd.py The current working directory is /home/frank/
Furthermore, the os module contains the additional getcwdb() method. This one is similar to the getcwd() method but returns the path as a binary string, instead.
There are quite a few other directory operations not covered in this article, like checking if a file or directory exists. But for now we’ll move on to the main purpose of this article.
Creating a Directory
Creating a single directory is done using the mkdir() method. As a parameter, mkdir() first requires the path name for the directory in order for it to be created. For an example, see the code below:
import os # define the name of the directory to be created path = "/tmp/year" try: os.mkdir(path) except OSError: print ("Creation of the directory %s failed" % path) else: print ("Successfully created the directory %s " % path)
Keep in mind that the mkdir() method cannot create sub-directories on a deeper level than one in a single call. To create an entire path you have to call mkdir() once per directory level. Alternatively, if you want to create multiple directories at once, make use of the makedirs() method instead (which you can see in Listing 4 below).
As an optional parameter you can specify the access rights to the directory within your mkdir() call. The default setting is 777, which means it is readable and writable by the owner, group members, and all other users as well. In case you require a more restrictive setting, like 755, (readable and accessible by all users, and write access by only the owner) you may call it as follows:
import os # define the name of the directory to be created path = "/tmp/year" # define the access rights access_rights = 0o755 try: os.mkdir(path, access_rights) except OSError: print ("Creation of the directory %s failed" % path) else: print ("Successfully created the directory %s" % path)
One thing to note about this code — you may have noticed that the access rights (755 here) are specified using the octal prefix ( 0o ), which is done so you don’t need to convert the number to decimal first. Since the OS represents the access permissions as octal, that’s how we’ll represent them here too.
However, as the Python documentation states, some systems ignore the mode parameter and you should use os.chmod instead.
Creating a Directory with Subdirectories
As already mentioned above, the mkdir() method allows us to create a single directory, only. To create multi-level subdirectories the makedirs() method comes into play. Actually, makedirs() is implemented in such a way that it calls mkdir() to create one directory after the next.
As a parameter makedirs() accepts the entire path to be created. This method is similar to the UNIX/Linux command mkdir -p . Listing 4 shows an example of how to use this method.
import os # define the name of the directory to be created path = "/tmp/year/month/week/day" try: os.makedirs(path) except OSError: print ("Creation of the directory %s failed" % path) else: print ("Successfully created the directory %s" % path)
Creating a Temporary Directory
So far, we’ve created permanent entries in the file system. For various reasons like parking data temporarily it can be necessary to just have a temporary directory. The tempfile module contains methods that handle such cases in a safe and consistent way.
Listing 5 shows an example that uses the TemporaryDirectory() method in combination with the with statement. After the with statement the temporary directory does not exist anymore because both the directory, and its contents have been entirely removed.
import tempfile # create a temporary directory with tempfile.TemporaryDirectory() as directory: print('The created temporary directory is %s' % directory) # directory and its contents have been removed by this point
Listing 6 shows the output of the Python script from Listing 5. In order to create temporary files, on UNIX/Linux systems the three directories /tmp, /var/tmp, and /usr/tmp are tried, and the first match of them is taken. In this current case it is the /tmp directory that is used.
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
$ python3 mkdir-temporary.py The created temporary directory is /tmp/tmpf6o0hy3c
Deleting a Directory
Deleting a directory is the opposite case of creating one. You can do that using the rmdir() method of the os module. rmdir() requires a path string that contains the directory name, and only deletes the deepest entry in the path string. Note that this only works when the directory is entirely empty. If it is not empty then an OSError is raised. Listing 7 shows the according Python code.
import os # define the name of the directory to be deleted path = "/tmp/year" try: os.rmdir(path) except OSError: print ("Deletion of the directory %s failed" % path) else: print ("Successfully deleted the directory %s" % path)
In case you would like to remove an entire directory tree the rmtree() method from the shutil module will help you with that task.
Conclusion
As you may have noted, handling directories is very simple in Python. It takes you only a few lines of code to create and to remove this kind of file entry.
Links and References
Acknowledgements
The author would like to thank Zoleka Hatitongwe for her support while preparing the article.