File Processing
File processing means reading data from files, creating files, modifying their contents, and saving information for later use. In AI applications, files are often used for documents, datasets, configuration files, logs, and user-uploaded content.
File processing is simply reading data from a file and working with it.
Python provides simple built-in tools for opening,
reading, writing, and closing files. The most important
function to learn first is open().
What Is File Processing?
A file is a place where information is stored permanently.
Examples include:
- Text files
- CSV files
- JSON files
- Images
- PDF documents
- Log files
Python can interact with many of these files.
File ↓ Python opens file ↓ Read / Write / Modify ↓ Save changes ↓ Close file
Opening a File
Python uses the built-in open() function
to open a file.
file = open("example.txt")
print(file)
If example.txt exists in the current
directory, Python opens it.
However, simply opening a file is not enough. We normally need to read its contents and then close it.
Reading a File
The read() method reads the contents of
the file.
file = open("example.txt")
content = file.read()
print(content)
file.close()
Suppose example.txt contains:
Hello Python Welcome to AI.
The output will be:
Hello Python Welcome to AI.
Using with open()
The better way to work with files is using
with open().
with open("example.txt") as file:
content = file.read()
print(content)
Python automatically handles closing the file when the
with block finishes.
This is safer and cleaner than manually calling
file.close().
File Modes
The second argument of open() tells Python
how you want to use the file.
"r" → Read "w" → Write "a" → Append "x" → Create a new file
The default mode is "r".
with open("example.txt", "r") as file:
content = file.read()
print(content)
Writing to a File
Use "w" when you want to write content to
a file.
with open("output.txt", "w") as file:
file.write("Hello Python")
If output.txt does not exist, Python
creates it.
If it already exists, "w" replaces its
existing contents.
This is an important point:
"w" → replaces existing content
Appending to a File
Use "a" when you want to add content to
the end of an existing file.
with open("output.txt", "a") as file:
file.write("\nNew line")
Unlike "w", append mode does not remove
the existing content.
Existing file Hello Python After append Hello Python New line
Reading Lines
You can read a file line by line.
with open("example.txt") as file:
for line in file:
print(line)
This is useful when processing large files because you don't necessarily need to load the entire file into memory at once.
You can also use readlines():
with open("example.txt") as file:
lines = file.readlines()
print(lines)
File Paths
A file does not always exist in the same directory as your Python program.
You can provide a path.
with open("data/products.txt") as file:
content = file.read()
print(content)
You can also use an absolute path, although relative paths are often easier to manage inside a project.
project/
│
├── main.py
│
└── data/
└── products.txt
File Encoding
Text files have an encoding that determines how characters are stored.
UTF-8 is a common choice for modern applications.
with open(
"example.txt",
"r",
encoding="utf-8"
) as file:
content = file.read()
print(content)
Specifying the encoding helps avoid problems when working with different languages and special characters.
Checking if a File Exists
Before processing a file, you may need to check whether it exists.
Python provides the pathlib module for
working with paths.
from pathlib import Path
file_path = Path("example.txt")
if file_path.exists():
print("File exists")
else:
print("File does not exist")
File exists
Using pathlib
pathlib provides a cleaner way to work
with files and directories.
from pathlib import Path
file_path = Path("example.txt")
print(file_path.name)
print(file_path.suffix)
print(file_path.parent)
For example:
example.txt .txt .
This becomes especially useful when an AI application needs to process many uploaded files.
Working with Directories
Python can also create directories.
from pathlib import Path
folder = Path("documents")
folder.mkdir(
exist_ok=True
)
exist_ok=True prevents an error if the
directory already exists.
You can then create a file inside it:
file_path = folder / "notes.txt"
file_path.write_text(
"Python for AI",
encoding="utf-8"
)
Reading and Writing with pathlib
For simple text files, pathlib can make
file processing very clean.
from pathlib import Path
file_path = Path("notes.txt")
file_path.write_text(
"Learning Python",
encoding="utf-8"
)
content = file_path.read_text(
encoding="utf-8"
)
print(content)
Learning Python
Processing CSV Files
CSV files are commonly used for tabular data.
Python provides the built-in csv module.
Suppose products.csv contains:
name,price Laptop,1200 Phone,800 Tablet,500
We can read it with:
import csv
with open(
"products.csv",
"r",
encoding="utf-8"
) as file:
reader = csv.DictReader(file)
for row in reader:
print(row["name"])
print(row["price"])
Laptop 1200 Phone 800 Tablet 500
File Processing in AI Applications
File processing becomes much more interesting when building AI applications.
Imagine a user uploads a text document.
User uploads document
↓
Python receives file
↓
Read file
↓
Extract text
↓
Clean text
↓
Send text to AI model
↓
Generate response
For example, an AI application could read a text file containing customer feedback and then send that text to an AI model for summarization.
Processing Large Files
Loading an extremely large file completely into memory can be inefficient.
Instead, process it line by line.
with open(
"large_file.txt",
"r",
encoding="utf-8"
) as file:
for line in file:
process(line)
This approach allows Python to process the file incrementally rather than creating one huge string containing the entire file.
Handling File Errors
Files may not exist, may be inaccessible, or may contain unexpected data.
Use try and except when
appropriate.
try:
with open(
"example.txt",
"r",
encoding="utf-8"
) as file:
content = file.read()
print(content)
except FileNotFoundError:
print("File not found")
except PermissionError:
print("Permission denied")
This prevents the entire application from crashing unexpectedly when a file cannot be accessed.
Async and File Processing
This connects directly to the previous Async Python lesson.
Standard Python file operations are synchronous. For many simple applications, that is completely fine.
The important lesson is not to make every file operation asynchronous just because async exists.
Use async where it actually solves a waiting or concurrency problem.
Mini Project — AI Document Reader
Let's build a small document reader that reads a text file and prepares its contents for an AI application.
Create this structure:
project/
│
├── main.py
│
└── documents/
└── article.txt
Put some text inside article.txt:
Python is a popular programming language. It is widely used in data science and AI.
Now create main.py:
from pathlib import Path
file_path = Path(
"documents/article.txt"
)
if not file_path.exists():
print("Document not found")
else:
content = file_path.read_text(
encoding="utf-8"
)
print("Document loaded successfully")
print()
print(content)
Document loaded successfully Python is a popular programming language. It is widely used in data science and AI.
In a real AI application, the next step could be
sending content to an AI model for
summarization, classification, question answering, or
information extraction.
What You Learned
- What file processing means
- How to open files
- How to read files
- How to write files
- How to append data
- How to read files line by line
- File modes
- File paths
- UTF-8 encoding
- Checking whether files exist
- Using
pathlib - Working with directories
- Processing CSV files
- Handling file errors
- Processing large files
- Using files inside AI applications
File processing is the foundation for working with real-world data.
AI applications rarely work only with hard-coded strings. They often receive documents, datasets, configuration files, logs, and user uploads. Python's file-processing tools let you safely read, modify, and prepare that information before sending it to an AI system.