Python File & Directory Operations

Python os module provides some operating system functions for file & directory processing. Below you can find some examples of these functions.

Creating a Directory

To create a directory you can use mkdir() function from os module.

When dealing with paths, you can use raw strings. A Raw string is a string with an ‘r’ (or ‘R’) prefix. Normally, you need to use the backslash escape character (\\) to create a path string.

In raw strings, backslash character is treated as a normal character (not an escape character). Thus, you can create path strings without using escape characters in raw strings.

import os

os.mkdir("D:\\test")              # Backslash escape character 
                                  # is used (\\)

os.mkdir(r"D:\test\subfolder")    # Raw string is used (r prefix 
                                  # before string)

Renaming a Directory

To rename a file or directory, you can use rename() function.

import os

os.rename(r"D:\test", r"D:\test-renamed")

Listing Files

To list files in a directory, listdir() function is used. This function returns the names of files and folders in the given directory.

Since Python 3.5, you can use scandir() function. scandir() function not only returns file/folder names, but it also returns file attribute information and has better performance than listdir() function.

In the example below, *.txt files under D:\test directory are listed using both listdir() and scandir() functions.

root_folder = r"D:\test"

# listdir() example
for filename in os.listdir(root_folder):
    if filename.endswith(".txt"):
        print(os.path.join(root_folder, filename))

# scandir() example
for entry in os.scandir(root_folder):
    if entry.path.endswith(".txt") and entry.is_file():
        print(entry.path)

Listing Files Recursively

Consider we have the following file structure under D:\test directory.

D:
└───test
    │   file1.txt
    │   file2.txt
    │   image1.jpg
    │   image2.png
    │
    └───subfolder1
        │   file3.txt
        │   file4.txt
        │
        └───subfolder2
                file5.txt

To list files in a directory and all subdirectories recursively, you can use iglob() function from glob module.

Usage of iglob() function is shown in the following code which lists *.txt files recursively.

Note that double asterisk is used in the search path (D:\test\**\*.txt). A double asterisk (**) indicates that files should be searched in the current directory and all subdirectories under this directory.

import glob

for filename in glob.iglob(r'D:\test\**\*.txt', recursive=True):
    print(filename)

# D:\test\file1.txt
# D:\test\file2.txt
# D:\test\subfolder1\file3.txt
# D:\test\subfolder1\file4.txt
# D:\test\subfolder1\subfolder2\file5.txt

Deleting a Directory

You can use rmtree() function from shutil module to delete a directory and all files/folders under this directory.

import shutil

shutil.rmtree(r"D:\test")

# Caution: rmtree() function will delete all files and folders
#          under specified directory

Renaming a File

rename() function is used to rename files and folders in Python.

import os

os.rename(r"D:\test\file1.txt", r"D:\test\file1_renamed.txt")

Copying a File

copy2() function from shutil module is used to copy a file. This function not only copies the file contents but also the metadata of the file (like file modification time).

import shutil

shutil.copy2(r"D:\test\file1.txt", r"D:\test\file1_copy.txt")

Deleting a File

remove() function can be used to delete a file.

import os

os.remove(r"D:\test\file1_copy.txt")
SHARE