File Handling in Python
File handling refers to the process of performing operations on a file, such as creating,
opening, reading, writing and closing it through a programming interface. It involves
managing the data flow between the program and the file system on the storage device,
ensuring that data is handled safely and efficiently.
Why do we need File Handling?
To store data permanently, even after the program ends.
To access external files like .txt, .csv, .json, etc.
To process large files efficiently without using much memory.
To automate tasks like reading configs or saving outputs.
To handle input/output in real-world applications and tools.
File Types
Python primarily differentiates between two fundamental types of files for handling
purposes:
Text Files:
These files contain human-readable characters, such as letters, numbers, and
symbols. Each line in a text file is typically terminated by a special End-of-Line (EOL)
character, which is \n (newline) by default in Python. Examples
include .txt files, .csv (Comma Separated Values) files, .json (JavaScript Object
Notation) files, and .xml files.
Binary Files:
These files store data in a non-human-readable binary format (sequences of 0s and
1s). They do not have line terminators and often require specific software to
interpret their content correctly. Examples include images (.jpg, .png), audio files
(.mp3, .wav), video files (.mp4), and compiled executable files.
Beyond these core types, Python interacts with various file formats based on their content
and purpose:
Python Source Code Files:
.py: Standard Python source code files.
.ipynb: Jupyter Notebook files, combining code, output, and markdown text.
.pyc: Compiled bytecode files, generated by the Python interpreter for faster
execution.
.pyd: Python dynamic modules, often compiled from C/C++ code.
Data Files:
.csv: Comma-separated values, for tabular data.
.json: Lightweight data-interchange format.
.xml: Extensible Markup Language, for structured data.
.xlsx, .xls: Microsoft Excel spreadsheet files.
.hdf5: Hierarchical Data Format, for large datasets.
.pkl: Pickle files, used for serializing and deserializing Python objects.
Document Files:
.pdf: Portable Document Format.
.docx, .doc: Microsoft Word documents.
.txt: Plain text files.
Python's built-in open() function allows you to open files in different modes
(read, write, append, binary, text), enabling interaction with these diverse file
types. Libraries like Pandas, JSON, and openpyxl provide specialized tools for
working with specific data file formats.
Operations on File
File operations are actions performed on files by a user or program, managed by the
operating system, and include creating, opening, reading, writing, closing, deleting,
repositioning, and renaming files. These operations allow users and applications to manage
data stored in a computer's file system, enabling them to create new files, access existing
data, modify content, and remove unnecessary files from storage devices.
1. Opening Files:
The open() function is used to open a file. It takes the file path and the desired mode as
arguments. Common modes include:
'r' (read): Opens for reading (default).
'w' (write): Opens for writing, overwriting existing content or creating a new file.
'a' (append): Opens for appending, adding content to the end of an existing file or
creating a new one.
'x' (create): Creates a new file, raising an error if it already exists.
'b' (binary): Used in conjunction with other modes (e.g., 'rb', 'wb') for binary file
operations.
Example:
file = open("example.txt", "r") # Open for reading
2. Reading Files:
Once opened, content can be read using methods like:
read(size): Reads a specified number of bytes or the entire content if size is omitted.
readline(): Reads a single line.
readlines(): Reads all lines into a list.
Example:
content = file.read()
print(content)
3. Writing Files:
Content can be written using:
write(string): Writes a string to the file.
writelines(list_of_strings): Writes a list of strings to the file.
Example:
file = open("new_file.txt", "w")
file.write("This is a new line.\n")
file.writelines(["Line 1\n", "Line 2\n"])
4. Appending to Files:
To add content without overwriting, open the file in append mode ('a').
Example:
file = open("existing_file.txt", "a")
file.write("This content is appended.")
5. Closing Files:
It is crucial to close files after operations to release system resources using
the close() method. The with statement is highly recommended as it automatically handles
file closing, even if errors occur.
Example (using with statement):
with open("example.txt", "r") as file:
content = file.read()
print(content)
6. Other Operations:
Python's os and shutil modules provide functions for more advanced file and directory
operations, including:
Renaming files (os.rename())
Deleting files (os.remove())
Copying files (shutil.copy())
Creating directories (os.mkdir(), os.makedirs())
Listing directory contents (os.listdir())
Opening a File in Python
To open a file, we can use open() function, which requires file-path and mode as
arguments:
Syntax: file = open('filename.txt', 'mode')
filename.txt: name (or path) of the file to be opened.
mode: mode in which you want to open the file (read, write, append, etc.).
Note: If you don’t specify the mode, Python uses 'r' (read mode) by default.
Basic Example: Opening a File
f = open("geek.txt", "r")
print(f)
This code opens the file demo.txt in read mode. If the file exists, it connects successfully,
otherwise, it throws an error.
Closing a File
It's important to close the file after you're done using it. file.close() method closes the file
and releases the system resources ensuring that changes are saved properly (in case of
writing)
file = open("geeks.txt", "r")
# Perform file operations
file.close()
Checking File Properties
Once the file is open, we can check some of its properties:
f = open("geek.txt", "r")
print("Filename:", f.name)
print("Mode:", f.mode)
print("Is Closed?", f.closed)
f.close()
print("Is Closed?", f.closed)
Output:
Filename: demo.txt
Mode: r
Is Closed? False
Is Closed? True
Explanation:
f.name: Returns the name of the file that was opened (in this case, "demo.txt").
f.mode: Tells us the mode in which the file was opened. Here, it’s 'r' which means read
mode.
f.closed: Returns a boolean value- Fasle when file is currently open otherwise True .
Reading a File
Reading a file can be achieved by file.read() which reads the entire content of the file.
After reading the file we can close the file using file.close() which closes the file after
reading it, which is necessary to free up system resources.
Example: Reading a File in Read Mode (r)
file = open("geeks.txt", "r")
content = file.read()
print(content)
file.close()
Output:
Hello world
GeeksforGeeks
123 456
Using with Statement
Instead of manually opening and closing the file, you can use the with statement, which
automatically handles closing. This reduces the risk of file corruption and resource
leakage.
Example: Let's assume we have a file named geeks.txt that contains text "Hello,
World!".
with open("geeks.txt", "r") as file:
content = file.read()
print(content)
Output:
Hello, World!
Handling Exceptions When Closing a File
It's important to handle exceptions to ensure that files are closed properly, even if an
error occurs during file operations.
try:
file = open("geeks.txt", "r")
content = file.read()
print(content)
finally:
file.close()
Output:
Hello, World!
Explanation:
try: Starts the block to handle code that might raise an error.
open(): Opens the file in read mode.
read(): Reads the content of the file.
finally: Ensures the code inside it runs no matter what.
close(): Safely closes the file to free resources.
What are the operations of a file?