Python Create Folder In Current Directory
Last Updated : Mar 11, 2024
In this article we will show you the solution of python create folder in current directory, Python's os.mkdir() function can be used to create a new folder in the current working directory.
By passing the name of the folder you want to create to the os.mkdir() function, a folder will be created.
In Python, the current directory is the one from which your script is running. The os.getcwd() function in Python can be used to get the current working directory.
The Python programming language supports file creation, writing, and reading. Binary files (written in binary language, 0s, and 1s) and text files (written in binary language, 0s, and 1s) can be handled by Python.
The ability to create files is not limited to deleting them when you are no longer working with them.
Creating directories programmatically is easy, but you must check to ensure that they don't exist already.
Don't ignore it. You'll have problems otherwise. We will now discuss the idea of how to create a folder in the current directory with an example.
Step By Step Guide On Python Create Folder In Current Directory :-
import os def main(): # Create directory dirName = 'tcdir' try: # Create target Directory os.mkdir(dirName) print("directory " , dirname , " created ") except FileExistsError: print("directory " , dirname , " already exist") if not os.path.exists(dirName): os.mkdir(dirName) print("directory " , dirname , " created ") else: print("directory " , dirname , " already exist") dirName = 'tcdir/temp2/temp' try: os.makedirs(dirName) print("directory " , dirname , " created ") except FileExistsError: print("directory " , dirname , " already exist") if not os.path.exists(dirName): os.makedirs(dirName) print("directory " , dirname , " created ") else: print("directory " , dirname , " already exist") if __name__ == '__main__': main()
- The main function should be defined.
- Assign the name 'tcdir' to the directory.
- In order to create the directory 'tcdir', use a try-except block. In this example, we create a directory using the OS.mkdir() method, and we print a message if the directory does not exist, if we catch the 'FileExistsError' exception.
- The os.path.exists() method can be used to determine if the directory 'tcdir' exists. You should create it if it does not exist and print a message if it does not exist. Print a message indicating that the directory already exists if it exists.
- You should set the directory name to 'tcdir/temp2/temp'.
- Creating the directory 'tcdir/temp2/temp' should be performed using the try-except block. Using os.makedirs() we create an entire directory and all its subdirectories, and if the directory already exists we catch and print the 'FileExistsError' exception.
- By using the os.path.exists() method, determine whether the 'tcdir/temp2/temp' directory exists. You have to create it if it does not already exist and print a message if it does not exist. In the event that it already exists, print a message stating that it already exists.
- Whenever the program is executed as the main program, call the main function.
Conclusion :-
As a result, we have successfully learned how to create a folder in the current directory with an example.
Managing directories is extremely simple in Python, as you may have noticed.
The creation and removal of such a file entry can be accomplished with a few lines of code.
I hope this article on python create folder in current directory helps you and the steps and method mentioned above are easy to follow and implement.