Skip to content

Lesson 06 — Python File Handling

Almost every Python application interacts with files.

Whether you’re:

  • Reading application logs
  • Parsing AWS CloudTrail events
  • Updating configuration files
  • Processing CSV reports
  • Reading JSON API responses
  • Writing automation reports
  • Storing scan results
  • Creating backups

…you’re working with files.

File handling is one of the most practical Python skills because nearly every automation script reads data from a file or writes data to one.

Throughout your Cloud, DevOps, AI, and Cybersecurity journey, you’ll regularly process log files, configuration files, reports, and datasets using Python.


After completing this lesson, you will be able to:

  • Understand file handling in Python.
  • Open, read, write, and append files.
  • Work safely with files using context managers.
  • Process text, CSV, and JSON files.
  • Handle file-related exceptions.
  • Work with directories using pathlib.
  • Apply file handling in real-world IT automation.

File Handling is the process of creating, reading, updating, and deleting files using Python.

Files allow programs to:

  • Store information permanently
  • Exchange data
  • Generate reports
  • Read configuration files
  • Process logs
  • Import and export data

Without file handling, information would be lost when a program exits.


File handling enables automation scripts to:

  • Read server inventories
  • Analyze security logs
  • Save scan results
  • Store application settings
  • Generate audit reports
  • Process datasets

Nearly every enterprise automation script interacts with files.


Open File
Read or Write Data
Process Information
Save Changes
Close File

Python simplifies this process using built-in functions.


Use the open() function.

file = open("notes.txt")

This opens the file using the default read mode.


Mode Purpose
r Read
w Write (overwrite)
a Append
x Create new file
rb Read Binary
wb Write Binary

Choosing the correct mode is important to avoid accidental data loss.


Example:

file = open("notes.txt", "r")
print(file.read())
file.close()

Output:

Welcome to GoHackersCloud Academy

file = open("notes.txt")
for line in file:
print(line)
file.close()

Reading line by line is useful for large files.


file = open("notes.txt")
lines = file.readlines()
print(lines)
file.close()

This returns a list containing each line.


file = open("notes.txt", "w")
file.write("Python File Handling")
file.close()

Warning: Write mode replaces the existing file contents.


Append adds data without removing existing content.

file = open("notes.txt", "a")
file.write("\nCloud Computing")
file.close()

file = open("report.txt", "x")
file.close()

Python creates the file only if it does not already exist.


Always close files after use.

file.close()

Closing a file releases system resources.


The safest way to work with files is using with.

with open("notes.txt", "r") as file:
print(file.read())

Advantages:

  • Automatically closes the file
  • Cleaner code
  • Safer resource management
  • Recommended for all new programs

Example:

with open("message.txt", "w") as file:
file.write("Welcome Student")

Text files are commonly used for:

  • Notes
  • Reports
  • Configuration
  • Documentation

CSV files store tabular data.

Example:

Name,Department
Rahul,Cloud
Anita,Cybersecurity

Python example:

import csv
with open("employees.csv") as file:
reader = csv.reader(file)
for row in reader:
print(row)

CSV files are widely used in reporting and data exchange.


import csv
with open("students.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Name","Course"])
writer.writerow(["Rohit","Cloud Security"])

JSON is commonly used by cloud APIs.

Example JSON:

{
"Cloud":"AWS",
"Region":"Mumbai"
}

Read JSON:

import json
with open("config.json") as file:
data = json.load(file)
print(data["Cloud"])

Output:

AWS

import json
config = {
"Cloud":"AWS",
"Region":"Mumbai"
}
with open("config.json","w") as file:
json.dump(config, file, indent=4)

Relative path:

notes.txt

Absolute path:

C:\Projects\Python\notes.txt

Relative paths improve portability across systems.


Python’s modern file library:

from pathlib import Path
file = Path("notes.txt")
print(file.exists())

Useful methods:

  • exists()
  • is_file()
  • is_dir()
  • mkdir()
  • unlink()

from pathlib import Path
for file in Path(".").iterdir():
print(file)

Useful for automation and inventory scripts.


Sometimes files do not exist.

Example:

try:
with open("missing.txt") as file:
print(file.read())
except FileNotFoundError:
print("File not found.")

Exception handling prevents programs from crashing unexpectedly.


Binary mode is used for:

  • Images
  • PDFs
  • Executables
  • ZIP Files

Example:

with open("photo.jpg","rb") as file:
data = file.read()

Cloud Engineers commonly work with:

  • JSON configuration files
  • Terraform state files
  • YAML templates
  • CloudTrail logs
  • CSV inventories

Example:

import json
with open("cloud_config.json") as file:
config = json.load(file)

DevOps Engineers process:

  • Deployment logs
  • Configuration files
  • CI/CD reports
  • Docker Compose files
  • Kubernetes YAML manifests

Automation depends heavily on reading and updating files.


Security professionals analyze:

  • Windows Event Logs (exported)
  • Linux log files
  • Firewall logs
  • IDS alerts
  • Threat intelligence feeds
  • IOC lists

Example:

with open("security.log") as file:
for line in file:
print(line)

AI developers work with:

  • CSV datasets
  • JSON annotations
  • Images
  • Text corpora
  • Model outputs

Python simplifies loading and processing these files.


Beginners often:

  • Forget to close files.
  • Use the wrong file mode.
  • Overwrite important data with "w".
  • Forget exception handling.
  • Use incorrect file paths.
  • Ignore encoding issues.

Using with statements helps avoid many of these problems.


A SOC Analyst needs to review failed login attempts.

with open("security.log") as file:
for line in file:
if "Failed Login" in line:
print(line)

This simple script quickly identifies relevant events from a large log file.


As a Python developer:

  • Use with open() whenever possible.
  • Handle exceptions gracefully.
  • Use relative paths when practical.
  • Store configuration separately from code.
  • Avoid hardcoding file paths.
  • Validate file contents before processing.
  • Use JSON for structured configuration.
  • Use CSV for tabular reports.

Well-designed file handling improves reliability and maintainability.


After completing this lesson, you should understand:

  • File handling.
  • File modes.
  • Reading files.
  • Writing files.
  • Appending data.
  • CSV processing.
  • JSON processing.
  • Context managers.
  • pathlib.
  • Exception handling.

Python File Handling enables applications to store, retrieve, and process information from files.

Whether you’re reading log files, processing cloud configuration, generating reports, or building automation tools, file handling is a core programming skill used across Cloud Computing, DevOps, AI, and Cybersecurity.

Mastering file operations prepares you to build practical automation scripts and enterprise applications.


➡️ Lesson 07 — Working with APIs

In the next lesson, you’ll learn how Python communicates with web services and cloud platforms using Application Programming Interfaces (APIs). You’ll explore REST APIs, HTTP methods, request and response formats, authentication, status codes, and use the requests library to interact with real-world services.