File Handling in Python
Table of Contents
- What Is File Handling?
- Types of Files
- Understanding File Paths in Python
- Opening Files — open() Function
- File Object Attributes
- Reading Data from Files
- Writing to Files
- Reading and Writing Together
- Closing Files — close()
- Using with Statement (Context Manager)
- File Pointer and Cursor Position
- Working with Binary Files
- Checking File Existence and Deletion
- Common File Operations Summary
- File Processing Examples
- Exception Handling in File Operations
- Working with CSV Files
- Working with JSON Files
- Working with os and pathlib Modules
- File Handling Best Practices
- Summary Table
- Conclusion
1. What Is File Handling?
When you run a Python program, the data it processes lives in temporary memory (RAM).
Once the program ends, that data is gone.
To store data permanently, we need files.
File handling allows programs to create, read, update, and delete files stored on your disk.
So, file handling = the interface between Python (the program) and the storage system (the file system).
2. Types of Files
Files can be divided into two main categories:
| File Type | Description | Example |
|---|---|---|
| Text Files | Contain plain, human-readable characters | .txt, .csv, .py, .html |
| Binary Files | Contain data in binary format (0s and 1s), not human-readable | .jpg, .mp3, .exe, .dat |
Understanding File Paths in Python
When working with files in Python—whether reading data, writing logs, or processing documents—it’s essential to know how to locate those files.
This is where file paths come in. A file path tells Python exactly where a file lives in your computer’s file system, so it knows which file to open, read, or modify.
In simple terms, a file path acts like a map that guides Python to your file’s location.
What Is a File Path?
A file path is a string that represents the address or location of a file or folder within your system. It tells Python how to navigate through the directory structure to find a specific file.
Every operating system (Windows, macOS, Linux) organizes files in a hierarchical structure, starting from a root directory, and each folder (also called a directory) can contain files or other subfolders.
Python interacts with this structure through file paths.
Types of File Paths
There are two main types of file paths in Python:
- Absolute Path
- Relative Path
Understanding the difference between the two is critical for writing flexible and portable programs.
1. Absolute Path
An absolute path provides the complete address to a file or folder starting from the root directory of your file system.
In other words, it gives Python the entire route to reach the file, regardless of where your script is running.
Example:
file = open("C:/Users/Saumya/Documents/example.txt", "r")
Here,
- C: → is the drive name (on Windows)
- /Users/Saumya/Documents/ → is the folder path
- example.txt → is the file name
So this path directs Python to a specific file inside your “Documents” folder, no matter where the Python script is currently located.
Key Points:
- Always starts from the root (like C:/ on Windows or / on macOS/Linux).
- Works the same regardless of the current working directory.
- Typically used for fixed or system-wide files.
When to Use Absolute Paths
- When your files are stored in a fixed location that doesn’t change.
- When working on scripts that depend on specific directory structures.
- For automation scripts that access system directories or logs.
2. Relative Path
A relative path specifies the location of a file in relation to the current working directory (CWD).
In simple terms, Python doesn’t need to know the full address—it just starts looking from wherever your program is currently running.
Example:
file = open("example.txt", "r")
Here, Python assumes that example.txt is located in the same folder as your Python script (or your current working directory).
You don’t need to specify the entire location.
Key Points:
- Starts from the current working directory, not the root.
- More portable — you can move the project to another computer without changing file paths.
- Ideal for small projects, especially when all related files are in the same folder.
Current Working Directory (CWD)
Every time you run a Python program, it has a Current Working Directory (CWD) — the folder where Python looks for files by default when using relative paths.
Python provides functions in the os module to get or change this directory.
1. Getting the Current Working Directory
To check where Python is currently “looking” for files, use os.getcwd():
import os
print(os.getcwd())
Output (example):
C:\Users\Saumya\Projects
This shows your current directory. If you open a file using a relative path, Python will look for it inside this folder.
2. Changing the Current Working Directory
If your file is not in the current directory, you can change the working directory using os.chdir():
import os
os.chdir("C:/Users/Saumya/Documents")
print("New directory:", os.getcwd())
Output:
New directory: C:\Users\Saumya\Documents
After this change, Python will treat C:/Users/Saumya/Documents as the new default folder for opening and saving files.
Combining Paths Correctly
Hardcoding file paths (using C:/...) can cause issues when your code runs on different operating systems (like Linux or macOS).
Different systems use different path separators:
- Windows → uses backslashes (\)
- Linux/macOS → uses forward slashes (/)
To handle paths in a cross-platform way, Python provides utilities in the os.path and pathlib modules.
Using os.path.join()
Instead of manually writing the path, you can join folder and file names safely using os.path.join():
import os
path = os.path.join("C:/Users/Saumya", "Documents", "example.txt")
print(path)
This automatically uses the correct separator for your operating system.
Using pathlib (Modern and Recommended)
The pathlib module (introduced in Python 3.4) provides an object-oriented way to handle paths.
from pathlib import Path
file_path = Path("C:/Users/Saumya/Documents/example.txt")
print(file_path.exists()) # Check if the file exists
You can also use relative paths easily:
relative_path = Path("example.txt")
print(relative_path.resolve()) # Converts relative path to absolute path
pathlib is preferred in modern Python because it is more readable, safer, and easier to work with than traditional string-based paths.
Why File Paths Matter
When working with data, scripts, or automation, handling file paths correctly is crucial because:
- A small mistake in the path can cause a FileNotFoundError.
- Using absolute paths may break portability (your script might not run on another computer).
- Using relative paths without understanding the working directory can lead to confusion.
By understanding absolute and relative paths—and learning how to check or change your current directory—you can make your programs more robust, flexible, and portable.
Quick Summary
| Type of Path | Starts From | Example | Use Case |
|---|---|---|---|
| Absolute Path | Root directory | C:/Users/Saumya/Documents/example.txt | When file location is fixed |
| Relative Path | Current working directory | example.txt | When working within project folder |
| CWD Functions | os.getcwd(), os.chdir() | Get/change directory | To manage file locations dynamically |
4. Opening Files — open() Function
Python uses the open() function to interact with files.
Syntax:
file_object = open(filename, mode)
Common Modes:
| Mode | Meaning | Description |
|---|---|---|
| 'r' | Read | Default mode; opens file for reading. Error if file doesn’t exist. |
| 'w' | Write | Opens for writing; creates new file or overwrites existing. |
| 'a' | Append | Opens for writing; adds data at end without overwriting. |
| 'r+' | Read & Write | Opens for both reading and writing. Error if not found. |
| 'w+' | Write & Read | Creates a new file or overwrites existing. |
| 'a+' | Append & Read | Opens file for appending and reading. |
| 'b' | Binary | Used with binary files (add to any mode, e.g., 'rb', 'wb'). |
| 't' | Text | Default mode (e.g., 'rt'). |
Example:
f = open("file.txt", "w") # open for writing
f.write("Hello, Python!") # write to file
f.close() # close file
5. File Object Attributes
When you open a file, Python returns a file object with properties.
Example:
f = open("file.txt", "r")
print("File name:", f.name)
print("Mode:", f.mode)
print("Closed:", f.closed)
f.close()
print("Closed:", f.closed)
Output:
File name: file.txt
Mode: r
Closed: False
Closed: True
6. Reading Data from Files
You can read a file’s content in different ways depending on your needs.
(a) read() – Reads the entire file
f = open("data.txt", "r")
data = f.read()
print(data)
f.close()
Output:
Hello world
This is Python
You can limit characters read:
f = open("data.txt", "r")
print(f.read(5)) # reads 5 characters
f.close()
(b) readline() – Reads one line at a time
f = open("data.txt", "r")
print(f.readline()) # reads first line
print(f.readline()) # reads second line
f.close()
(c) readlines() – Reads all lines into a list
f = open("data.txt", "r")
lines = f.readlines()
print(lines)
f.close()
Output:
['Hello world\n', 'This is Python\n']
(d) Iterating through a file
Files are iterable, so you can read line by line:
with open("data.txt", "r") as f:
for line in f:
print(line.strip())
7. Writing to Files
When writing, Python creates a new file or overwrites the existing one.
(a) Using write()
f = open("output.txt", "w")
f.write("This is the first line.\n")
f.write("This is the second line.")
f.close()
(b) Using writelines()
Accepts a list of strings.
f = open("lines.txt", "w")
f.writelines(["Line 1\n", "Line 2\n", "Line 3\n"])
f.close()
(c) Using 'a' (Append mode)
f = open("output.txt", "a")
f.write("\nAdding another line.")
f.close()
8. Reading and Writing Together
To read and write simultaneously:
f = open("data.txt", "r+")
print(f.read())
f.write("\nAdded new content")
f.close()
9. Closing Files — close()
Always close your files after use:
f = open("data.txt", "r")
data = f.read()
f.close()
When you call close(), all system buffers are flushed, ensuring that data is properly written.
10. Using with Statement (Context Manager)
The best way to work with files is the with statement because it automatically closes the file after execution.
with open("data.txt", "r") as f:
content = f.read()
print(content)
# file automatically closed here
This method is recommended for all real-world applications.
11. File Pointer and Cursor Position
Every file object maintains a cursor (read/write pointer) internally.
- tell() → Returns the current position (in bytes).
- seek(offset, from_what) → Moves the cursor.
Example:
with open("data.txt", "r") as f:
print(f.read(5)) # read first 5 chars
print("Cursor at:", f.tell())
f.seek(0) # reset cursor to start
print(f.read(5))
Output:
Hello
Cursor at: 5
Hello
12. Working with Binary Files
Binary files deal with data in bytes. You can read or write binary data using 'rb' and 'wb'.
Example – Copy an image:
with open("source.jpg", "rb") as src:
with open("copy.jpg", "wb") as dest:
dest.write(src.read())
This reads bytes from one image and writes them to another.
13. Checking File Existence and Deletion
You can check if a file exists and delete it using os.
import os
if os.path.exists("old.txt"):
os.remove("old.txt")
else:
print("File not found")
Create or remove directories:
os.mkdir("NewFolder")
os.rmdir("NewFolder")
14. Common File Operations Summary
| Function | Description |
|---|---|
| open() | Open file |
| read() | Read all or specific bytes |
| readline() | Read one line |
| readlines() | Read all lines into list |
| write() | Write string |
| writelines() | Write multiple lines |
| seek() | Move cursor |
| tell() | Return cursor position |
| close() | Close file |
| with open() | Automatically closes file |
15. File Processing Examples
Example 1: Count lines, words, and characters
with open("notes.txt", "r") as f:
text = f.read()
lines = text.splitlines()
words = text.split()
print("Lines:", len(lines))
print("Words:", len(words))
print("Characters:", len(text))
Example 2: Reverse file content
with open("data.txt", "r") as f:
lines = f.readlines()
with open("data.txt", "w") as f:
for line in reversed(lines):
f.write(line)
16. Exception Handling in File Operations
Files might not exist or could be corrupted.
Use try-except blocks for safe handling.
try:
with open("missing.txt", "r") as f:
print(f.read())
except FileNotFoundError:
print("Error: File not found.")
except IOError:
print("Input/output error.")
17. Working with CSV Files
CSV (Comma-Separated Values) are simple text files for tabular data.
Writing CSV
import csv
with open("people.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Name", "Age", "City"])
writer.writerow(["Saumya", 23, "Delhi"])
writer.writerow(["Riya", 25, "Pune"])
Reading CSV
import csv
with open("people.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
print(row)
Output:
['Name', 'Age', 'City']
['Saumya', '23', 'Delhi']
['Riya', '25', 'Pune']
18. Working with JSON Files
JSON is a format for storing structured data.
Writing JSON
import json
data = {"name": "Saumya", "age": 23, "languages": ["Python", "C++"]}
with open("data.json", "w") as f:
json.dump(data, f)
Reading JSON
import json
with open("data.json", "r") as f:
data = json.load(f)
print(data)
Output:
{'name': 'Saumya', 'age': 23, 'languages': ['Python', 'C++']}
19. Working with os and pathlib Modules
Both modules help interact with files and directories.
Using os:
import os
print(os.getcwd()) # current directory
print(os.listdir()) # list all files/folders
os.rename("old.txt", "new.txt") # rename file
Using pathlib:
from pathlib import Path
path = Path("notes.txt")
if path.exists():
print("File size:", path.stat().st_size)
20. File Handling Best Practices
- Always use with open() — automatic cleanup.
- Always handle exceptions with try-except.
- Avoid hardcoding file paths. Use os.path.join() for portability.
- Never assume file existence — always check first.
- For large files, read in chunks instead of loading all at once.
Example:
with open("large_file.txt", "r") as f:
while chunk := f.read(1024):
print(chunk)
21. Summary Table
| Concept | Description |
|---|---|
| File | Persistent storage of data |
| open() | Open file for operations |
| Modes | 'r', 'w', 'a', 'r+', 'b', 't' |
| read() | Reads entire file |
| write() | Writes string to file |
| seek() / tell() | Manage file pointer |
| with open() | Automatically closes file |
| os, pathlib | File & directory utilities |
| csv, json | Specialized file handling |
| try-except | Handle runtime errors safely |
22. Conclusion
- File handling is essential for building real-world applications — logging, data storage, configuration, and data processing.
- Python’s built-in tools make it simple yet powerful, with modules like os, csv, and json.
- Mastering file handling ensures your programs can persist and interact with data reliably.
