os.makedirs()
os.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. For example consider the following path:
/home/User/Documents/w3wiki/Authors/nikhil
Suppose we want to create a directory ‘nikhil’ but Directory ‘w3wiki’ and ‘Authors’ are unavailable in the path. Then os.makedirs() method will create all unavailable/missing directories in the specified path. ‘w3wiki’ and ‘Authors’ will be created first then ‘nikhil’ directory will be created.
Example:
Python3
# Python program to explain os.makedirs() method # importing os module import os # Leaf directory directory = "nikhil" # Parent Directories parent_dir = "/home/User/Documents/w3wiki/Authors" # Path path = os.path.join(parent_dir, directory) # Create the directory # 'ihritik' os.makedirs(path) print ( "Directory '%s' created" % directory) |
Output:
Directory 'nikhil' created
10 Python File System Methods You Should Know
While programming in any language, interaction between the programs and the operating system (Windows, Linux, macOS) can become important at some point in any developer’s life. This interaction may include moving files from one location to another, creating a new file, deleting a file, etc.