Essential File and Directory Operations in Python
Core Path Operations
Path Concatenation
Use the / operator for path joining: path / 'subdirectory'.
Initialize paths directly: Path("dir1", "dir2", "filename.txt").
Path Format Conversion
Convert to POSIX-style string: path_obj.as_posix().
Working Directory Management
Current Directory Inspection
import os
print(os.getcwd())
Directory Modification
Change working directory: os.chdir(target_path).
Script Directory Retrieval
Using pathlib:
from pathlib import Path
script_dir = Path(__file__).parent
Using os module:
import os
script_dir = os.path.dirname(__file__)
Set working directory to script's parent:
os.chdir(os.path.dirname(__file__))
String Path Manipulations
Path Joining and Name Extraction
Append components: path_obj.joinpath('new_element').
Extract final component: path_obj.name.
Windows Path Notation
Use raw strings for Windows paths:
win_path = r"C:\Users\Example\Documents\data.txt"
Directory Creation
Recursive Directory Creation
target_path.mkdir(parents=True)
Single-Level Directory Creation
os.mkdir(dir_name)
Path Information Retrieval
Absolute Path Convertion
absolute_path = os.path.abspath(path_string)
Existence Verification
from pathlib import Path
item_path = Path("/target/location")
if item_path.is_file():
# File exists
if item_path.is_dir():
# Directory exists
if item_path.exists():
# Item exists
File Operations
Extension Extraction
filename, extension = os.path.splitext(file_string)
Basename Acquisition
base_name = os.path.basename(full_path)
Filename Without Extension
from pathlib import Path
name_only = Path(__file__).stem
Text File Processing
Reading lines efficiently:
with open(text_file, "r") as file_handle:
for line_content in file_handle:
process(line_content)
Writing multiple lines:
with open(output_file, "w") as file_handle:
file_handle.writelines(line_collection)
JSON File Handling
Writing JSON data:
import json
with open(json_file, "w") as file_handle:
json.dump(data_structure, file_handle)
Appending JSON data:
with open(json_file, "a") as file_handle:
json.dump(additional_data, file_handle)
Reading JSON data:
with open(data_file, "r") as file_handle:
loaded_data = json.load(file_handle)
Directory Management
Directory Removal
import shutil
if os.path.isdir(target_directory):
shutil.rmtree(target_directory)
Directory Validation
assert os.path.isdir("expected/path")
Immediate Children Iteration
Using pathlib:
parent = Path(directory_path)
for child_item in parent.iterdir():
print(child_item)
Using os module:
for item_name in os.listdir(root_directory):
print(item_name)
Recursive Directory Traversal
for current_root, subdirs, file_names in os.walk(start_path):
for file_name in file_names:
print(current_root)
print(os.path.join(current_root, file_name))
File Pattern Matching
Wildcard Patterns
*: Matches zero or more characters?: Matches exactly one character[a-z]: Matches any character in the specified range
Pattern Matching Implementation
import glob
matching_files = glob.glob("directory/*.extension")
Iterative Pattern Matching
glob.iglob() returns an iterator enstead of a complete list.
File Comparison
import filecmp
try:
identical = filecmp.cmp("first.txt", "second.txt")
if identical:
print("Files match")
else:
print("Files differ")
except OSError:
print("File access error occurred")
Temporary File Handling
The tempfile module provides temporary file creation utilities.
Archive Operations
ZIP File Extraction
import zipfile
with zipfile.ZipFile('archive.zip') as zip_archive:
zip_archive.extractall('destination/')