How to Operate Files in Python Using os.path and pathlib Module

Operating files is a fundamental task in programming, and Python provides powerful modules like `os.path` and `pathlib` to handle file operations efficiently. In this article, we’ll explore various methods to perform common file operations such as checking file existence, determining file types, managing permissions, and creating new files and directories.

1. Check File/Folder Existence.

There are multiple ways to check the existence of a file or directory:

1.1 Using os.path.exists method.

This method checks whether a given path exists or not.

import os, stat
import pathlib

# Check file/folder existence by os.path.exists method.
def check_file_exist_by_os_path(file_path):

    ret = False
    # If this file object exist.
    if(os.path.exists(file_path)):
        ret = True
        print(file_path + " exist.")
        # If this is a file.
        if(os.path.isfile(file_path)):
            print(" and it is a file.")
        # This is a directory.    
        else:
            print(" and it is a directory.")
    else:
        ret = False
        print(file_path + " do not exist.")
        
    return ret     
        
if __name__ == '__main__':
    file_folder = "./test"    
    file_path = file_folder + "/abc.txt"
    # Check file existence.
    fileExist = check_file_exist_by_os_path(file_path)

Output1.

./test/abc.txt do not exist.

Output2.

./resource-files/data.csv exist.
 and it is a file.

1.2 Catching FileNotFoundError exception.

Utilize exception handling to catch errors when attempting to access non-existing files.

import os, stat
import pathlib

# Check file/folder existence by exception.         
def check_file_exist_by_exception(file_path):
    ret = True
    try:
        # Open file object.
        file_object = open(file_path, 'r')
        # Read entire file content data.
        file_data = file_object.read()
        print(file_path + " exist. It's data : " + file_data)
    except FileNotFoundError:
        ret = False
        print(file_path + " do not exist.")
    except IOError:
        ret = False
        print(file_path + " can not be read. ")    
    except PermissionError:
        ret = False
        print("Do not have permission to read file " + file_path)
    finally:
        return ret
     
        
if __name__ == '__main__':
    #file_folder = "./test"
    file_folder = "./resource-files"
    
    #file_path = file_folder + "/abc.txt"
    file_path = file_folder + "/data.csv"
    # Check file existence.
    fileExist = check_file_exist_by_exception(file_path)

Output1.

./resource-files/data.csv exist. It's data : 1.2,3.4,5.6,7.8
9.0,2.1,4.3,6.5
8.7,6.5,4.3,2.1
0.1,2.3,4.5,6.7

Output2.

./test/abc.txt do not exist.

1.3 Using pathlib module.

This module offers a convenient way to interact with file paths and check their existence.

import os, stat
import pathlib

# Check file/folder existence by pathlib.         
def check_file_exist_by_pathlib(file_path):
    ret = True
        
    # Create path lib object.
    pl = pathlib.Path(file_path)
    
    # Check whether the path lib exist or not.
    ret = pl.exists()
    
    if(ret):
        print(file_path + " exist.")
    else:
        print(file_path + " do not exist.")
    
    if(pl.is_file()):
        print(file_path + " is a file.")
       
    if(pl.is_dir()):
        print(file_path + " is a directory.")
    
    return ret
     
        
if __name__ == '__main__':
    file_folder = "./test"
    #file_folder = "./resource-files"
   
    file_path = file_folder + "/abc.txt"
    #file_path = file_folder + "/data.csv"
    # Check file existence.
    fileExist = check_file_exist_by_pathlib(file_path)

Output1.

./test/abc.txt do not exist.

Output2.

./resource-files/data.csv exist.
./resource-files/data.csv is a file.

2. Check If A Path Is a Directory Or File.

Determining whether a given path points to a directory or a file is essential. You can achieve this using the following methods:

2.1 os.path.isfile method.

Checks if a given path corresponds to a regular file.

2.2 pathlib.is_file or pathlib.is_dir method.

The `is_file` method checks if the path points to a file, while `is_dir` verifies if it’s a directory.

3. Check File Readable, Writable, Or Executable Status.

To ascertain the status of file permissions, Python provides the `os.access` method. It enables you to check if a file is readable, writable, or executable.

import os, stat
import pathlib

# Check file/folder status by os.access method.
def check_file_status_by_os_access(file_path):
    # If this file exist.
    if(os.access(file_path, os.F_OK)):
        print(file_path + " exist.")
    else:
        print(file_path + " do not exist.")
            
    if(os.access(file_path, os.R_OK)):
        print(file_path + " is readable.")
        
    if(os.access(file_path, os.W_OK)):
        print(file_path + " is writable.")    
        
    if(os.access(file_path, os.EX_OK)):
        print(file_path + " is executable.")
     
        
if __name__ == '__main__':
    file_folder = "./resource-files"
    file_path = file_folder + "/data.csv"
    check_file_status_by_os_access(file_path)

Output1.

./resource-files/data.csv exist.
./resource-files/data.csv is readable.
./resource-files/data.csv is writable.
./resource-files/data.csv is executable.

4. Create New Directory.

Python’s `os.mkdir` method allows you to create new directories. Before creating a directory, it’s advisable to check if it already exists to prevent overwriting.

import os, stat
import pathlib

# Check file/folder existence by os.path.exists method.
def check_file_exist_by_os_path(file_path):

    ret = False
    # If this file object exist.
    if(os.path.exists(file_path)):
        ret = True
        print(file_path + " exist.")
        # If this is a file.
        if(os.path.isfile(file_path)):
            print(" and it is a file.")
        # This is a directory.    
        else:
            print(" and it is a directory.")
    else:
        ret = False
        print(file_path + " do not exist.")
        
    return ret

    
# Create a new directory.
def create_new_folder(file_path):
    if(not check_file_exist_by_os_path(file_path)):
        os.mkdir(file_path)
        print(file_path + " has been created. ")

        
if __name__ == '__main__':
    file_folder = "./test"
    create_new_folder(file_folder)
    check_file_status_by_os_access(file_folder)

Output.

./test do not exist.
./test has been created. 
./test exist.
./test is readable.
./test is writable.
./test is executable.

5. Create New File.

Creating a new file involves opening a file object and writing content into it. This can be achieved using Python’s file-handling capabilities.

import os, stat
import pathlib

# Check file/folder existence by os.path.exists method.
def check_file_exist_by_os_path(file_path):

    ret = False
    # If this file object exist.
    if(os.path.exists(file_path)):
        ret = True
        print(file_path + " exist.")
        # If this is a file.
        if(os.path.isfile(file_path)):
            print(" and it is a file.")
        # This is a directory.    
        else:
            print(" and it is a directory.")
    else:
        ret = False
        print(file_path + " do not exist.")
        
    return ret


# Check file/folder status by os.access method.
def check_file_status_by_os_access(file_path):
    # If this file exist.
    if(os.access(file_path, os.F_OK)):
        print(file_path + " exist.")
    else:
        print(file_path + " do not exist.")
            
    if(os.access(file_path, os.R_OK)):
        print(file_path + " is readable.")
        
    if(os.access(file_path, os.W_OK)):
        print(file_path + " is writable.")    
        
    if(os.access(file_path, os.EX_OK)):
        print(file_path + " is executable.")

# Create a new file and write some text in it.
def create_new_file(file_path):
    file_object = open(file_path, 'w')
    file_object.write('File is created.')
    print(file_path + " has been created. ")
     
        
if __name__ == '__main__':
    file_folder = "./test"
    file_path = file_folder + "/abc.txt"
    # Check file existence.
    fileExist = check_file_exist_by_os_path(file_path)
    # If file do not exist then create it.
    if(not fileExist):
        create_new_file(file_path)

    check_file_status_by_os_access(file_folder)

Output.

./test/abc.txt do not exist.
./test/abc.txt has been created. 
./test exist.
./test is readable.
./test is writable.
./test is executable.

6. Change File Permission.

Python’s `os.chmod` method facilitates changing file permissions. You can specify the desired permissions using predefined constants provided by the `stat` module.

import os, stat
import pathlib

# Check file/folder status by os.access method.
def check_file_status_by_os_access(file_path):
    # If this file exist.
    if(os.access(file_path, os.F_OK)):
        print(file_path + " exist.")
    else:
        print(file_path + " do not exist.")
            
    if(os.access(file_path, os.R_OK)):
        print(file_path + " is readable.")
        
    if(os.access(file_path, os.W_OK)):
        print(file_path + " is writable.")    
        
    if(os.access(file_path, os.EX_OK)):
        print(file_path + " is executable.")


# Change the file permission to read and execute only.
def set_file_permission(file_path):
    os.chmod(file_path, stat.S_IREAD | stat.S_IWRITE )
     
        
if __name__ == '__main__':
    file_folder = "./test"
    
    file_path = file_folder + "/abc.txt"

    set_file_permission(file_path)

    check_file_status_by_os_access(file_folder)

Output.

./test exist.
./test is readable.
./test is writable.
./test is executable.

7. Conclusion.

By incorporating these methods into your Python scripts, you can efficiently manage files and directories, perform necessary checks, and manipulate file permissions as needed. Understanding these techniques is invaluable for proficient file handling in Python.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.