File handling in Python, is a fundamental skill enabling developers to create, read, update, and delete files stored on a system. Python provides built-in functions and modules for handling files effectively, making it a versatile choice for tasks involving data storage and manipulation.
File handling refers to the process of creating, reading, writing, and manipulating files on a storage system. It is a common functionality in programming for tasks like saving user data, processing logs, and working with configurations. Here’s a brief overview of file handling in different programming languages:
Python simplifies file handling, enabling users to effortlessly read, write, and perform a variety of operations on files. Unlike other programming languages where file handling can be complicated or verbose, Python offers a straightforward and concise approach, making it an ideal choice for beginners and professionals alike.
Table of Contents
File Handling in Python
If you want to dive deeper into Python file handling, our Python Self-Paced Course is the perfect resource. It covers everything from basic to advanced Python concepts, helping you build a strong foundation.
Python categorizes files as either text files or binary files, and this distinction is crucial. A text file comprises a sequence of characters, with each line ending in a special character known as the End of Line (EOL) character, such as a comma (,
) or newline character. These characters signify the end of one line and the start of a new one.
Types of Files – File Handling in Python,
Python supports handling different types of files, primarily:
- Text Files
- Contain readable characters organized into lines.
- Examples:
.txt
,.csv
,.json
- Binary Files
- Contain binary data that is not human-readable.
- Examples:
.jpg
,.exe
,.dat
Opening Files
To work with a file, it first needs to be opened. Python provides the open()
function, which takes two main arguments:
- File Name: The name of the file to open.
- Mode: Specifies the purpose for opening the file.
Modes in open()
Mode | Description |
---|---|
r | Read (default mode). |
w | Write (creates or truncates). |
x | Create (fails if file exists). |
a | Append to the file. |
b | Binary mode. |
t | Text mode (default). |
+ | Read and write. |
Example:
file = open("example.txt", "r")
Reading Files – File Handling in Python
Methods to Read:
read()
: Reads the entire file or a specified number of characters.
with open("example.txt", "r") as file:
content = file.read()
print(content)
readline()
: Reads a single line from the file.
with open("example.txt", "r") as file:
line = file.readline()
print(line)
readlines()
: Reads all lines into a list.
with open("example.txt", "r") as file:
lines = file.readlines()
print(lines)
Writing Files
Methods to Write:
write()
: Writes a string to the file.
with open("example.txt", "w") as file:
file.write("Hello, World!")
writelines()
: Writes a list of strings.
with open("example.txt", "w") as file:
file.writelines(["Hello\n", "World\n"])
Appending Files – File Handling in Python
Using the a
mode, you can add content without overwriting existing data.
with open("example.txt", "a") as file:
file.write("Appending new content.\n")
Closing Files – Handling the Files in Python
Although Python’s with
statement automatically closes files, you can use file.close()
explicitly.
Example:
file = open("example.txt", "r")
content = file.read()
file.close()
File Iteration
Files can be iterated over line by line using a loop:
with open("example.txt", "r") as file:
for line in file:
print(line.strip())
Working with Binary Files
Binary files require the b
mode for operations.
Example:
with open("example.jpg", "rb") as file:
binary_data = file.read()
To write binary data:
with open("output.jpg", "wb") as file:
file.write(binary_data)
Handling File Exceptions
Python provides robust exception handling for file operations using try-except
blocks.
Example:
try:
with open("example.txt", "r") as file:
content = file.read()
except FileNotFoundError:
print("File not found.")
except IOError:
print("IO error occurred.")
File Methods and Attributes
file.name
: Returns the file name.file.mode
: Returns the mode in which the file was opened.file.closed
: Checks if the file is closed.
Example:
file = open("example.txt", "r")
print(file.name) # example.txt
print(file.mode) # r
file.close()
print(file.closed) # True
File Paths
Python supports:
- Absolute Paths: Complete path starting from the root directory.
- Relative Paths: Path relative to the current working directory.
Example:
# Absolute Path
open("C:/Users/Username/Documents/example.txt", "r")
# Relative Path
open("./example.txt", "r")
Deleting Files
Python’s os
module is used to delete files:
Example:
import os
os.remove("example.txt")
To check if the file exists before deletion:
if os.path.exists("example.txt"):
os.remove("example.txt")
else:
print("File does not exist.")
Working with Directories
Python’s os
module provides functions for directory handling:
os.mkdir()
: Creates a new directory.os.rmdir()
: Removes an empty directory.os.listdir()
: Lists all files and directories.
Example:
import os
# Create a directory
os.mkdir("new_folder")
# List contents
print(os.listdir("."))
# Remove a directory
os.rmdir("new_folder")
Advanced File Handling
File Handling with shutil
Python’s shutil
module supports advanced file operations, such as copying and moving files.
Example:
import shutil
# Copy file
shutil.copy("example.txt", "copy_example.txt")
# Move file
shutil.move("example.txt", "new_directory/example.txt")
Working with pathlib
The pathlib
module offers object-oriented file and directory manipulation.
Example:
from pathlib import Path
# Create a Path object
file_path = Path("example.txt")
# Check if file exists
if file_path.exists():
print("File exists.")
# Read file content
print(file_path.read_text())
File Compression
Python supports file compression using the zipfile
and gzip
modules.
Using zipfile
Example:
import zipfile
# Create a ZIP file
with zipfile.ZipFile("example.zip", "w") as zipf:
zipf.write("example.txt")
# Extract a ZIP file
with zipfile.ZipFile("example.zip", "r") as zipf:
zipf.extractall("output_folder")
Using gzip
Example:
import gzip
# Compress file
with open("example.txt", "rb") as f_in:
with gzip.open("example.txt.gz", "wb") as f_out:
f_out.writelines(f_in)
# Decompress file
with gzip.open("example.txt.gz", "rb") as f_in:
with open("example_dec.txt", "wb") as f_out:
f_out.writelines(f_in)
Summary
Python’s file handling capabilities are extensive, offering developers powerful tools for interacting with files and directories. With robust error handling, advanced modules like pathlib
and shutil
, and support for various file types and formats, Python makes it easy to manage and manipulate data stored on disk.
Read This also