Project Structure
Working with Files
2 min read
File operations are essential for AI projects—reading prompts, saving responses, managing configurations, and processing data.
Reading Files
# Basic file reading with context manager
with open("prompt.txt", "r") as f:
content = f.read()
# Read lines into a list
with open("data.txt", "r") as f:
lines = f.readlines() # Includes \n
# Or: lines = f.read().splitlines() # No \n
# Read line by line (memory efficient)
with open("large_file.txt", "r") as f:
for line in f:
process(line.strip())
Writing Files
# Write text to file
with open("output.txt", "w") as f:
f.write("Hello, World!\n")
f.write("Second line\n")
# Append to existing file
with open("log.txt", "a") as f:
f.write(f"New entry: {message}\n")
# Write multiple lines
lines = ["Line 1", "Line 2", "Line 3"]
with open("output.txt", "w") as f:
f.writelines(line + "\n" for line in lines)
Using pathlib (Modern Approach)
from pathlib import Path
# Create path object
file_path = Path("data") / "prompts" / "system.txt"
# Check if exists
if file_path.exists():
content = file_path.read_text()
# Write to file
output_path = Path("output") / "response.txt"
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text("AI response here")
# List files in directory
for file in Path("prompts").glob("*.txt"):
print(file.name)
Common Path Operations
from pathlib import Path
path = Path("/home/user/project/data/file.json")
path.name # "file.json"
path.stem # "file"
path.suffix # ".json"
path.parent # Path("/home/user/project/data")
path.exists() # True/False
path.is_file() # True/False
path.is_dir() # True/False
# Join paths
config_path = Path("config") / "settings.json"
# Get absolute path
absolute = path.resolve()
Practical AI Example
from pathlib import Path
import json
class PromptManager:
def __init__(self, prompts_dir="prompts"):
self.prompts_dir = Path(prompts_dir)
self.prompts_dir.mkdir(exist_ok=True)
def save_prompt(self, name, content):
path = self.prompts_dir / f"{name}.txt"
path.write_text(content)
def load_prompt(self, name):
path = self.prompts_dir / f"{name}.txt"
if path.exists():
return path.read_text()
return None
def list_prompts(self):
return [p.stem for p in self.prompts_dir.glob("*.txt")]
# Usage
manager = PromptManager()
manager.save_prompt("system", "You are a helpful assistant.")
prompts = manager.list_prompts()
File Encodings
# Specify encoding (important for non-ASCII text)
with open("arabic.txt", "r", encoding="utf-8") as f:
content = f.read()
# Or with pathlib
content = Path("arabic.txt").read_text(encoding="utf-8")
Next, we'll learn regular expressions for text processing. :::