In this article we will show you the solution of create directory in python, Python allows you to create directories (also called folders) according to your needs. As part of this course, you will also learn how to create nested directories.
Working with directories in Python requires you to include the os module in your project, which allows you to interact with your operating system.
Several applications were built with Python, some of which required a directory to be created before they could run. We will now discuss the idea of how to add python to path in python with an example.
Step By Step Guide On Create Directory In Python :-
import os def main(): # Create directory dirName = 'tempDir' try: # Create target Directory os.mkdir(dirName) print("Directory " , dirname , " created ") except FileExistsError: print("Directory " , dirname , " already available") # Create target Directory if don't exist if not os.path.exists(dirName): os.mkdir(dirName) print("Directory " , dirname , " created ") else: print("Directory " , dirname , " already available") dirName = 'tempDir2/temp2/temp' # Create target directory & all intermediate directories if don't exists try: os.makedirs(dirName) print("Directory " , dirname , " created ") except FileExistsError: print("Directory " , dirname , " already available") # Create target directory & all intermediate directories if don't exists if not os.path.exists(dirName): os.makedirs(dirName) print("Directory " , dirname , " created ") else: print("Directory " , dirname , " already available") if __name__ == '__main__': main()
- Import the built-in Python module "os" which provides a way to interact with the operating system.
- Define a function named "main()" which will be called when the script is run.
- Assign the directory name 'tempDir' to a variable named "dirName".
- Try to create the directory using the command "os.mkdir(dirName)".
- If the directory already exists, catch the resulting "FileExistsError" exception and print a message saying that the directory is already available.
- Use the "os.path.exists()" function to check if the directory exists.
- If the directory doesn't exist, create it using the command "os.mkdir(dirName)".
- If the directory already exists, print a message saying that the directory is already available.
- Change the directory name to 'tempDir2/temp2/temp' and assign it to the variable "dirName".
- Try to create the directory and all intermediate directories using the command "os.makedirs(dirName)".
- If any of the intermediate directories already exist, catch the resulting "FileExistsError" exception and print a message saying that the directory is already available.
- Use the "os.path.exists()" function to check if the directory exists.
- If the directory doesn't exist, create it and all intermediate directories using the command "os.makedirs(dirName)".
- If the directory already exists, print a message saying that the directory is already available.
- Call the "main()" function if the script is run directly (i.e. not imported as a module).
Conclusion :-
As a result, we have successfully learned how to add python to path in python with an example.
Here we have demonstrated how to create a directory by using the mkdir() method, which will create it if all parent directories on the path are present, otherwise we can use the makedirs() method to create all intermediate-level directories automatically.
Both methods can be used as part of an OS module in Python.
I hope this article on create directory in python helps you and the steps and method mentioned above are easy to follow and implement.