Fundamentals 6 min read

10 Essential Python File Operation Libraries and Their Usage

This article introduces ten Python libraries—including os, pathlib, shutil, glob, zipfile, tarfile, csv, json, configparser, and logging—that simplify file handling tasks such as checking existence, reading, writing, copying, compressing, and logging, and provides clear code examples for each.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
10 Essential Python File Operation Libraries and Their Usage

In daily API testing, handling files such as login credentials and database dumps is common; this article introduces ten Python libraries that simplify file operations and provides concise usage examples.

os : Provides functions to interact with the operating system, including file and directory manipulation, process management, etc.

import os
# Check if file exists
if os.path.exists('file.txt'):
    print('File exists')
# Get file size
file_size = os.path.getsize('file.txt')
# Delete file
os.remove('file.txt')

pathlib : Offers an object‑oriented approach to handle filesystem paths.

from pathlib import Path
# Check if file exists
if Path('file.txt').exists():
    print('File exists')
# Get file size
file_size = Path('file.txt').stat().st_size
# Delete file
Path('file.txt').unlink()

shutil : Supplies high‑level operations for copying, moving, and removing directories.

import shutil
# Copy file
shutil.copy('source.txt', 'destination.txt')
# Move file
shutil.move('source.txt', 'destination.txt')
# Remove directory and its contents
shutil.rmtree('directory')

glob : Enables filename pattern matching to locate files.

import glob
# Find all .txt files
txt_files = glob.glob('*.txt')
# Find all .py files in subdirectories recursively
py_files = glob.glob('**/*.py', recursive=True)

zipfile : Allows creation, reading, and extraction of ZIP archives.

import zipfile
# Create ZIP archive
with zipfile.ZipFile('archive.zip', 'w') as zip:
    zip.write('file.txt')
# Extract ZIP archive
with zipfile.ZipFile('archive.zip', 'r') as zip:
    zip.extractall('destination_folder')

tarfile : Handles creation, reading, and extraction of TAR (including gzip‑compressed) archives.

import tarfile
# Create TAR.GZ archive
with tarfile.open('archive.tar.gz', 'w:gz') as tar:
    tar.add('file.txt')
# Extract TAR.GZ archive
with tarfile.open('archive.tar.gz', 'r:gz') as tar:
    tar.extractall('destination_folder')

csv : Provides utilities to read from and write to CSV files.

import csv
# Read CSV file
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
# Write CSV file
with open('data.csv', 'w') as file:
    writer = csv.writer(file)
    writer.writerow(['Name', 'Age'])
    writer.writerow(['John', 30])
    writer.writerow(['Alice', 25])

json : Enables encoding and decoding of JSON data.

import json
# Read JSON file
with open('data.json', 'r') as file:
    data = json.load(file)
    print(data)
# Write JSON file
data = {'name': 'John', 'age': 30}
with open('data.json', 'w') as file:
    json.dump(data, file)

configparser : Facilitates reading and writing of INI configuration files.

import configparser
# Read config file
config = configparser.ConfigParser()
config.read('config.ini')
value = config.get('Section', 'Option')
# Write config file
config = configparser.ConfigParser()
config['Section'] = {'Option': 'Value'}
with open('config.ini', 'w') as file:
    config.write(file)

logging : Offers flexible, configurable logging of application events.

import logging
logging.basicConfig(filename='app.log', level=logging.INFO)
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')

These libraries cover a wide range of file‑related tasks, enabling developers to efficiently read, write, copy, compress, and log files within Python projects.

JSONLibrariesOSCSVshutilfile operationspathlib
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.