All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Create Folder In Current Directory

Last Updated : Aug 2, 2023

Python Create Folder In Current Directory

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()
  1. The main function should be defined.
  2. Assign the name 'tcdir' to the directory.
  3. 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.
  4. 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.
  5. You should set the directory name to 'tcdir/temp2/temp'.
  6. 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.
  7. 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.
  8. 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.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪