File Operations Reference

Complete reference for Python file operations, modes, and methods. This covers all aspects of file handling including opening, reading, writing, and managing files and directories.

File Opening Modes

ModeDescriptionReadWriteCreateTruncatePositionBinary
'r'Read only (default)Start
'w'Write onlyStart
'a'Append onlyEnd
'x'Exclusive creation✓*Start
'r+'Read and writeStart
'w+'Read and writeStart
'a+'Read and appendEnd/Start**
'rb'Read binaryStart
'wb'Write binaryStart
'ab'Append binaryEnd
'xb'Exclusive binary✓*Start
'rb+'Read/write binaryStart
'wb+'Read/write binaryStart
'ab+'Read/append binaryEnd/Start**

*Fails if file exists | **Reading starts at beginning, writing at end

Mode Examples

# Basic file reading
with open('data.txt', 'w') as f:
    f.write("Hello World\nSecond Line")

with open('data.txt', 'r') as f:
    content = f.read()
    print(content)
# Basic file modes
with open('data.txt', 'r') as f:         # Read text file
    content = f.read()

with open('data.txt', 'w') as f:         # Write text file (overwrites)
    f.write("Hello World")

with open('data.txt', 'a') as f:         # Append to text file
    f.write("New line")

# Binary modes
with open('image.jpg', 'rb') as f:       # Read binary file
    data = f.read()

with open('output.jpg', 'wb') as f:      # Write binary file
    f.write(data)

# Read and write modes
with open('data.txt', 'r+') as f:        # Read and write existing file
    content = f.read()
    f.write("Additional text")

# Exclusive creation
try:
    with open('new_file.txt', 'x') as f: # Create new file (fails if exists)
        f.write("New file content")
except FileExistsError:
    print("File already exists")

File Object Methods

Reading Methods

MethodPurposeExampleReturn TypeDescription
read()Read entire filef.read()str/bytesRead all remaining content
read(size)Read specific bytesf.read(100)str/bytesRead up to size characters
readline()Read single linef.readline()str/bytesRead next line including newline
readlines()Read all linesf.readlines()listRead all lines into list

Writing Methods

MethodPurposeExampleReturn TypeDescription
write()Write string/bytesf.write("text")intWrite content, return characters written
writelines()Write list of linesf.writelines(lines)NoneWrite sequence of strings
flush()Force write to diskf.flush()NoneFlush internal buffer to disk

Position Methods

MethodPurposeExampleReturn TypeDescription
tell()Get current positionf.tell()intReturn current file position
seek()Change positionf.seek(0)intMove to specific position
seek(offset, whence)Relative seekf.seek(10, 1)intMove relative to whence (0=start, 1=current, 2=end)

File State Methods

MethodPurposeExampleReturn TypeDescription
close()Close filef.close()NoneClose file and free resources
closedCheck if closedf.closedboolTrue if file is closed
readable()Check if readablef.readable()boolTrue if file supports reading
writable()Check if writablef.writable()boolTrue if file supports writing
seekable()Check if seekablef.seekable()boolTrue if file supports seeking

File Reading Examples

# Read with position tracking
with open('data.txt', 'w') as f:
    f.write("Hello World!")

with open('data.txt', 'r') as f:
    print(f"Position: {f.tell()}")  # 0
    first_5 = f.read(5)
    print(f"Read: {first_5}")
    print(f"Position: {f.tell()}")  # 5
# Read entire file
with open('data.txt', 'r') as f:
    content = f.read()
    print(content)

# Read specific number of characters
with open('data.txt', 'r') as f:
    chunk = f.read(100)  # Read first 100 characters
    
# Read line by line
with open('data.txt', 'r') as f:
    for line in f:  # Most efficient way to iterate
        print(line.strip())

# Read all lines into list
with open('data.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        print(line.rstrip('\n'))

# Read with position tracking
with open('data.txt', 'r') as f:
    print(f"Position: {f.tell()}")  # 0
    first_10 = f.read(10)
    print(f"Position: {f.tell()}")  # 10
    f.seek(0)  # Back to beginning
    print(f"Position: {f.tell()}")  # 0

File Writing Patterns

Basic Writing

# Write string to file
with open('output.txt', 'w') as f:
    f.write("Hello World\n")
    f.write("Second line\n")

# Write multiple lines
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
with open('output.txt', 'w') as f:
    f.writelines(lines)

# Write with formatting
data = {"name": "Alice", "age": 25}
with open('output.txt', 'w') as f:
    f.write(f"Name: {data['name']}\n")
    f.write(f"Age: {data['age']}\n")

Append Mode

# Append to existing file
with open('log.txt', 'a') as f:
    f.write(f"[{datetime.now()}] New log entry\n")

# Append with data preservation
def log_message(message, filename='app.log'):
    with open(filename, 'a', encoding='utf-8') as f:
        timestamp = datetime.now().isoformat()
        f.write(f"[{timestamp}] {message}\n")

log_message("Application started")
log_message("User login successful")

File System Operations (os module)

Path Operations

FunctionPurposeExampleReturn TypeDescription
os.path.exists()Check if path existsos.path.exists('file.txt')boolTrue if path exists
os.path.isfile()Check if fileos.path.isfile('file.txt')boolTrue if path is a file
os.path.isdir()Check if directoryos.path.isdir('folder')boolTrue if path is a directory
os.path.getsize()Get file sizeos.path.getsize('file.txt')intFile size in bytes
os.path.getmtime()Get modification timeos.path.getmtime('file.txt')floatLast modification timestamp
os.path.join()Join path componentsos.path.join('dir', 'file.txt')strPlatform-appropriate path
os.path.split()Split pathos.path.split('/dir/file.txt')tuple(directory, filename)
os.path.splitext()Split extensionos.path.splitext('file.txt')tuple(name, extension)
os.path.dirname()Get directory nameos.path.dirname('/dir/file.txt')strDirectory part of path
os.path.basename()Get base nameos.path.basename('/dir/file.txt')strFilename part of path
os.path.abspath()Get absolute pathos.path.abspath('file.txt')strAbsolute path

Directory Operations

FunctionPurposeExampleReturn TypeDescription
os.listdir()List directory contentsos.listdir('.')listList of files and directories
os.mkdir()Create directoryos.mkdir('new_dir')NoneCreate single directory
os.makedirs()Create directory treeos.makedirs('dir/subdir')NoneCreate directory and parents
os.rmdir()Remove empty directoryos.rmdir('empty_dir')NoneRemove empty directory
os.removedirs()Remove directory treeos.removedirs('dir/subdir')NoneRemove empty directories
os.getcwd()Get current directoryos.getcwd()strCurrent working directory
os.chdir()Change directoryos.chdir('/path/to/dir')NoneChange working directory

File Operations

FunctionPurposeExampleReturn TypeDescription
os.remove()Delete fileos.remove('file.txt')NoneDelete a file
os.rename()Rename file/directoryos.rename('old.txt', 'new.txt')NoneRename file or directory
os.stat()Get file statisticsos.stat('file.txt')os.stat_resultDetailed file information

Modern Path Handling (pathlib)

Path Creation and Properties

Method/PropertyPurposeExampleReturn TypeDescription
Path()Create path objectPath('file.txt')PathCreate Path object
Path.cwd()Current directoryPath.cwd()PathCurrent working directory
Path.home()Home directoryPath.home()PathUser home directory
exists()Check existencepath.exists()boolTrue if path exists
is_file()Check if filepath.is_file()boolTrue if path is a file
is_dir()Check if directorypath.is_dir()boolTrue if path is a directory
stat()Get file statspath.stat()os.stat_resultFile statistics
suffixFile extensionpath.suffixstrFile extension
stemFilename without extensionpath.stemstrFilename without extension
nameFull filenamepath.namestrComplete filename
parentParent directorypath.parentPathParent directory path

Path Operations

MethodPurposeExampleReturn TypeDescription
joinpath()Join pathspath.joinpath('subdir', 'file.txt')PathJoin path components
with_suffix()Change extensionpath.with_suffix('.bak')PathChange file extension
with_name()Change filenamepath.with_name('new.txt')PathChange filename
resolve()Resolve absolute pathpath.resolve()PathAbsolute path
relative_to()Get relative pathpath.relative_to(base)PathPath relative to base

Error Handling in File Operations

Common File Exceptions

ExceptionCauseExampleSolution
FileNotFoundErrorFile doesn't existReading non-existent fileCheck existence first
PermissionErrorAccess deniedWriting to read-only fileCheck permissions
IsADirectoryErrorExpected file, got directoryOpening directory as fileCheck if it's a file
FileExistsErrorFile already existsCreating existing file with 'x' modeUse different mode or check existence
OSErrorGeneral OS errorDisk full, invalid pathHandle specific OS conditions

File Processing Patterns

Large File Processing

# Process large files efficiently
def process_large_file(filename, chunk_size=8192):
    """Process large file in chunks to avoid memory issues"""
    try:
        with open(filename, 'r') as f:
            while True:
                chunk = f.read(chunk_size)
                if not chunk:
                    break
                
                # Process chunk
                process_chunk(chunk)
                
    except Exception as e:
        print(f"Error processing large file: {e}")

def process_chunk(chunk):
    # Your processing logic here
    pass

# Line-by-line processing for text files
def process_text_file_lines(filename):
    """Efficiently process text file line by line"""
    try:
        with open(filename, 'r') as f:
            for line_number, line in enumerate(f, 1):
                # Process each line
                processed_line = line.strip().upper()
                print(f"Line {line_number}: {processed_line}")
    except Exception as e:
        print(f"Error processing file lines: {e}")

File Backup and Versioning

import shutil
from datetime import datetime

def create_backup(filename):
    """Create timestamped backup of file"""
    if not os.path.exists(filename):
        return None
    
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    name, ext = os.path.splitext(filename)
    backup_name = f"{name}_backup_{timestamp}{ext}"
    
    try:
        shutil.copy2(filename, backup_name)
        return backup_name
    except Exception as e:
        print(f"Backup failed: {e}")
        return None

def safe_file_write(filename, content):
    """Write file with backup"""
    backup = None
    if os.path.exists(filename):
        backup = create_backup(filename)
    
    try:
        with open(filename, 'w') as f:
            f.write(content)
        print(f"File written successfully: {filename}")
        return True
    except Exception as e:
        print(f"Write failed: {e}")
        if backup:
            print(f"Backup available: {backup}")
        return False

Best Practices

  • Always use context managers: Use with statement for automatic file closing
  • Handle exceptions appropriately: Anticipate and handle file-related errors
  • Choose correct file modes: Understand the differences between text and binary modes
  • Use pathlib for path operations: More modern and cross-platform than os.path
  • Process large files efficiently: Read in chunks to avoid memory issues
  • Validate file paths: Check existence and permissions before operations
  • Use appropriate encodings: Specify encoding explicitly for text files
  • Close files properly: Even with context managers, be aware of resource management
  • Consider atomic operations: Use temporary files for critical data operations

Was this helpful?

😔Poor
🙁Fair
😊Good
😄Great
🤩Excellent