Creating a folder in Python: How to do it easily

Creating Folders in Python: How to Easily Do It

In Python, folders are also called directories. We can use the os or os.path modules to create, delete, and rename folders (directories). This article details how to create folders using Python. Let’s take a look.

Creating Folders with the os Module

os provides many functions for interacting with the operating system, including creating, deleting, moving, and renaming files and directories. Let’s explore each one below.

First, we’ll use the os.mkdir function to create a single-level directory. This function requires a string argument, which is the path to the directory to be created. The sample code is as follows:

import os
dir_path = "test_dir"
os.mkdir(dir_path)

In the above code, we pass in a string argument, dir_path, which represents the name of the folder to be created. Note that if a folder with the same name already exists at this path, the function will throw an OSError exception.

Next, we’ll explain how to create multiple directories. If the directory hierarchy to be created is deep, it would be cumbersome to pass in all the directories at once. In this case, you can use the os.makedirs function to create directories recursively. The example code is as follows:

import os
dir_path = "./test_dir/test_subdir/test_subsubdir"
os.makedirs(dir_path)

In the above code, we create three levels of directories. The last path, test_subsubdir, is treated as a third directory nested within the second directory, test_subdir. Using the os.makedirs function to recursively create multiple levels of directories is common, convenient, and error-prone.

Creating Folders with the os.path Module

os.path contains many functions related to file paths, including creating and deleting folders (directories). Let’s explore them below.

First, we’ll use the os.path.join function to create directories. The os.path.join module function concatenates multiple path strings and automatically identifies the operating system type. The return value is a string representing the concatenated path. Example code is as follows:

import os
dir_path = os.path.join(".", "test_dir")
os.mkdir(dir_path)

The above code uses the os.path.join function to concatenate two path strings (“. and “test_dir”) together. The concatenated directory path is the directory “test_dir” in the current directory.

Another interesting tip: if you want to create a temporary directory in the current directory, you can use tempfile.TemporaryDirectory to conveniently accomplish this. You can also let Python’s garbage collection mechanism automatically clean up the directory when appropriate. The example code is as follows:

import tempfile
import shutil

with tempfile.TemporaryDirectory() as tmp_dir:
print(tmp_dir)

The above code uses the tempfile.TemporaryDirectory function, which creates a folder in a temporary directory and returns the folder’s path. When you leave the with block, Python automatically deletes the temporary directory.

Creating Folders with the shutil Module

shutil is a file manipulation module library that comes with Python and provides many convenient file manipulation features. This module provides the make_archive function to package folders into a .zip file and automatically retrieve the folder name. See the example below.

import shutil

shutil.make_archive("test_dir", 'zip', "test_dir")

The above code uses the shutil.make_archive function, which returns the full path to the compressed file. The first argument is the name of the compressed file, the second is the compression type (commonly used are “zip”, “tar”, and “gztar”), and the third is the path to the folder to be archived.

Conclusion

This article introduced three different ways to create folders in Python: using the os module, the os.path module, and the shutil module. os.mkdir and os.makedirs can create single-level and multi-level directories, os.path.join can concatenate multiple path strings to generate a path, and shutil.make_archive can compress folders into a compressed file.

When you need to create a new directory, you can choose different methods based on your specific situation to achieve the best results.

Leave a Reply

Your email address will not be published. Required fields are marked *