G
GuideDevOps
Lesson 4 of 15

Working with Files

Part of the Python for DevOps tutorial series.

Reading and writing files is a core task in DevOps, from parsing system logs to generating configuration files for Nginx or Docker.

1. Reading Files

The recommended way to open files is using the with statement, which ensures the file is properly closed even if an error occurs.

Read Entire File

Action:

with open('server.list', 'r') as f:
    content = f.read()
    print(content)

Result:

web-01
web-02
db-01

Read Line by Line (Memory Efficient)

For large log files, reading line by line is better for performance.

Action:

with open('app.log', 'r') as f:
    for line in f:
        print(f"LOG: {line.strip()}")

Result:

LOG: 2026-04-10 INFO: Starting app...
LOG: 2026-04-10 ERROR: Connection timeout

2. Writing Files

Write and Append

Use 'w' to overwrite a file and 'a' to append to the end of it.

Action:

# Overwrite
with open('config.txt', 'w') as f:
    f.write("PORT=8080\n")
 
# Append
with open('config.txt', 'a') as f:
    f.write("DEBUG=true\n")
 
# Verify
with open('config.txt', 'r') as f:
    print(f.read())

Result:

PORT=8080
DEBUG=true

3. File and Directory Operations

The os and shutil modules provide high-level operations for files and directories.

Action:

import os
import shutil
 
# Check if file exists
if os.path.exists('config.txt'):
    print("File found!")
 
# Copy file
shutil.copy('config.txt', 'config.bak')
print(f"Backup created: {os.path.exists('config.bak')}")
 
# Get file size
size = os.path.getsize('config.txt')
print(f"Size: {size} bytes")

Result:

File found!
Backup created: True
Size: 21 bytes

4. Modern Paths with pathlib

pathlib is the modern, object-oriented way to handle file paths in Python 3.

Action:

from pathlib import Path
 
path = Path("logs") / "app.log"
 
print(f"Filename: {path.name}")
print(f"Suffix:   {path.suffix}")
print(f"Is file:  {path.is_file()}")

Result:

Filename: app.log
Suffix:   .log
Is file:  False

Summary

  • with open(...) is the only safe way to handle files.
  • Use 'a' mode for logging to avoid overwriting existing data.
  • Use shutil for moving and copying files.
  • pathlib is preferred over os.path for new scripts.