How to Create a Folder in Python

Save

Learning how to interact with a file system is a very valuable programming skill. Many programs need to be capable of reading and writing files and folders to various file systems. The Python language has a library called "os," which provides functionality for creating folders. The "os" library can create folders for the Windows or Unix file systems. The syntax for creating folders in Python is easy to learn and can greatly increase you programming capability.

  • Open the IDLE text editor that comes with the Python programming language. A blank source-code file opens in the main editor window.

  • Import the "os" library by writing the following line of code at the top of the page:

    import os

  • Declare a variable that will hold the name and path of the folder. You can leave out the path and just write a folder name, but this will create the folder in the same directory as your source code. By specifying a path, you can create the folder anywhere. For example, you could write this to create a folder named "test" on the C: drive:

    directoryPath = r'C:\test'

  • Make a directory using the "mkdir" function. You pass in the directory name variable like this:

    os.mkdir(directoryPath)

  • Run the program by pressing the F5 key. A new directory named "test" is created in the same directory as your source code file.

References

Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!