Handling Text Files in Python
Introduction to File Handling
Data is at the core of every modern application, and managing it efficiently is essential. Think of a banking app that keeps track of transactions, a messaging app that records conversations, or a music app that remembers your playlist—all of these rely on efficient file management. This is where Python’s file handling comes into play. We can process big datasets and store information across sessions by reading, writing, and managing data saved in files.
This article covers important methods such as opening, reading, writing, and closing files to examine how Python’s file management facilitates effective data storage and retrieval.
Understanding file modes in Python
In Python, selecting the right file mode is essential, as it determines how a file will be accessed and modified. Each file mode has a specific purpose, from reading existing data to creating new content.
Let’s go through the various file handling modes, providing an overview of each and how they’re used to manage file operations effectively.
- ‘r’ (Read Mode): The read mode is the default for file handling. It allows you to read a file. If the file does not exist, it raises a ‘FileNotFoundError’ exception.
- ‘w’ (Write Mode): The character “w” denotes Write Mode. It opens a file for writing. If a file already exists, its content is erased (reduced to zero length) before writing new data. If the file doesn’t exist, it creates a new file.
- ‘a’ (Append Mode): Opens a file for adding new data at the end without affecting the existing content.
- ‘x’ (Exclusive Creation Mode): This mode allows a person to create a new file. If the file already exists, it raises a
FileExistsError
error. -‘b’ (Binary Mode): The binary mode is responsible when a user wants to open a binary version of the file. It is commonly used to handle binary files such as images, audio, video, documents, etc. This can be used in combination with other modes (e.g., ‘rb’, ‘wb’). - ‘t’ (Text Mode): This mode opens the file in text mode by default, where the contents are handled as strings. It can also be combined with other modes, such as ‘rt’ for reading or ‘wt’ for writing.
- ‘r+’ (Read/Write Mode): The read/write mode opens a file for both reading and writing. It won’t delete the file’s content if it already exists, which means that the file’s existing contents are not erased. Instead of clearing the file’s content, the new data will simply be added at the end of the existing content.
- ‘w+’ (Write/Read Mode): This mode is responsible for both writing and reading. If a file already exists, it is reduced to zero length; otherwise, a new file is created.
- ‘a+’ (Append/Read Mode): Opens a file for appending and reading both. If the file already exists, the current information can still be read even though the data is written at the end of the file.
Each mode serves a specific purpose, ensuring we can effectively handle files and their operations. Next, we’ll outline the key file handling methods that will be covered in this article.
How to Handle Files in Python
Opening Files in Python
The open()
function is a built-in function in Python that is essential for file access and manipulation. It allows you to read, write, and append data to a file, making it the first step in interacting with external files in Python.
Let’s take a look at the syntax of the open()
function:
file_object = open('filename', 'mode')
Let’s walk through the parameters of the open()
function. This function is used in Python to access and manipulate files, allowing you to read, write, and append data, making it the first step when working with external files :
file_object
: Holds the file object.filename
: Path name of the file.mode
: Specifies the operation to perform on the file (e.g., -‘r’ for reading, ‘w’ for writing). This parameter is optional, but it’s recommended that the correct mode be specified for proper file handling.
Reading from a File
Reading from a file in Python allows you to access and work with data stored externally. Python provides several methods for reading file data, giving you flexibility depending on how you want to process file content. Below, we’ll explore some common methods for reading data and how to use them effectively.
The example below demonstrates using the open()
function to open a file in Python:
file = open('example.txt', 'r') content = file.read()print(content)file.close()
The contents of the file example.txt
will be opened in read mode:
Hello, world!This is a file opening example.
Now that we understand how to open files, let’s move on to reading content from a file.
read()
The read()
method reads the entire file or a specified number of characters and returns it as a string. Let’s look at an example of how the read()
method works:
with open('example.txt', 'r') as file:content = file.read(12)print(content)
In this example, read(12)
reads the first 12 characters of the file.
readline()
The readline()
method reads the file line by line, returning the first line as a string, including the newline character. This method is useful for processing files one line at a time.
with open('example.txt', 'r') as file:print(file.readline())
Here the readline()
method reads only the first line of the file example.txt.
Writing to a File
Writing to a file is one of the most important aspects of interacting with files in Python. It enables us to record information, save data, and save results for future use. The open()
method specifies the mode in which you want to access a file when you want to add new data or replace old data. Python offers a variety of writing modes.
Let’s explore how you can write data to a file using Python’s write()
and writelines()
methods.
write()
The write()
method allows the person to write a single string to a file. It’s important to remember that write () does not add newline characters automatically, so we’ll need to insert \n
if you want entries to appear on new lines.
Let’s try understanding the write()
method with the help of an example:
#opens a file in writing modewith open('example.txt', 'w') as file:# Writes a string followed by a new linefile.write('Hello, world!\n')# Writes another stringfile.write('Python is great!\n')
The output will be:
Hello, world!Python is great!
The above code opens the file example.txt in write mode, writes two lines of text (“Hello, world!” and “Python is great!”), and then automatically closes the file and erases any existing information.
writelines()
The writelines()
method is used to write a sequence of strings (like a list of strings) to a file. Unlike write()
, it does not add newline characters automatically. Therefore, if the user wants every item on a new line, they will need to make sure that every string in the list ends with \n
.
Let’s walk through an example of how to use the writelines()
method.
#creates a list containing two stringslines = ['Hello, world! \n', 'Python is great! \n']#opens a file in writing modewith open ('example.txt', 'w') as file:#writes the list of strings in the listfile.writelines(lines)
The output will be:
Hello, world!Python is great!
In this example, writelines()
writes each string in the list lines
to example.txt. Remember, writelines()
does not automatically add line breaks, so include \n
at the end of each string if needed.
Difference between ‘w’ and ‘a’
There are two main writing modes in Python for handling file-writing operations:
a
(Append Mode):
This mode opens the file for writing, but it does not truncate the file; instead, new data is appended to it at the end. It creates a new file in case there is no existing file. This mode is useful when a person wants to add information to an existing filewithout erasing its original contents.
Let’s look at an example to better understand the append
mode:
#opens a file in write modewith open('example.txt', 'a') as file:#appends new datafile.write('Appending new content\n')
In the above code, the file example.txt already contains the data, so the new content will be appended at the end.
w
(Write Mode):
When the file is opened for writing in Write Mode, it truncates it (removes the present content) if it already exists before writing new data. A new file is created if the existing one doesn’t exist. This mode is helpful if you want to add new data to a file or replace all of the content in an existing file.
Let’s walk through an example for a better understanding of the write
mode:
# Opens a file in write modewith open('example.txt', 'w') as file:# Writes new data, replacing any existing contentfile.write('This is new content\n')
The code above will replace the file example.txt with the text “This is new content.” This new content would be added to the file in place of any existing data, if any.
Now that we’ve learned the fundamentals of Python’s reading, writing, and file modes, let’s move on to error management, the importance of file closure, and other recommended file-handling techniques.
Best Practices for File Handling
Effective file management is crucial in Python to prevent data loss, corruption, and memory issues. By handling files properly, we ensure smooth program execution and data integrity. Let’s dive into some essential file-handling practices:
File Encoding
File encoding defines how characters are stored and read across different systems ensuring consistent data representation and interpretation. It is mostly used for non-Latin alphabets and special symbols, and also supports all Unicode characters, including ASCII and UTF-8, the most popular encoding suitable for multilingual text.
Correct encoding ensures data integrity and prevents the files from data corruption, loss, or misreading of characters.
To demonstrate how file encoding functions in Python, let’s write the string “Hello, World!” to a file using UTF-8 encoding. We then read the file back to make that the encoding was handled appropriately.
# Write text with UTF-8 encodingwith open('example.txt', 'w', encoding = 'utf-8') as file:file.write("Hello, world!")# Read the text back with UTF-8 encodingwith open('example.txt', 'r', encoding = 'utf-8') as file:content = file.read()print(content)
Error Handling
Error handling is essential for managing unexpected issues when working with files, such as missing files or access permissions. Common file-handling errors include:
FileNotFoundError
: Raised when a file is missingPermissionError
: Raised when there are insufficient permissions.ValueError
: Raised when a file is opened in invalid mode.IOError
: Raised for input/output operation failures.
For error handling, Python uses try
and except
blocks. The try
block contains the code that can raise an error, whereas the except
block identifies the error that is to be caught and manages it. Optionally, the finally
block can be used to ensure that some code (like closing a file) is always executed.
Let’s look at an example and understand this in detail:
try:with open('data.txt', 'r') as file:data = file.read() # This error occurs when file is not foundexcept FileNotFoundError:print("Error: The file 'data.txt' was not found.")except PermissionError:print("Error: You do not have permission to read 'data.txt'.")except Exception as e:print(f"An unexpected error occurred: {e}")finally:print("This will run no matter what, to close resources if needed.")
After discussing file path management and error handling, it’s essential to focus on correctly closing up files after use.
Closing of Files
Incorrect file closure might result in problems with system resources or data loss. Python’s close()
function guarantees that all data is written to disk and that the file is released. As an alternative, employing the with
statement causes the file to be automatically closed after operations are finished. This ensures that data is saved accurately, resources are released, and future file access problems are prevented.
In Python, there are two possible ways to close a file: using the close()
method directly, and opening a file using with()
(the file is closed automatically when the code block is finished executing).
Close()
Method
After performing a set of operations on a file, we can manually close the file using the close()
method. It is one of the basic methods for file handling.
Here is how we can use the close()
method:
file = open('example.txt', 'w')file.write('Hello, world!')# Manually closes the file after writingfile.close()
The above code snippet opens the file example.txt in write mode, writes the content into the file and then manually closes it using the close()
method.
Using the with()
statement
The `with’ statement ensures automatically closing the file once the code is executed. This assures proper resource management in files.
Let us examine this by looking at the below example code snippet:
with open('example.txt', 'w') as file:file.write('Hello, world!')
In the above code the file example.txt directly gets closed after the file operations are done, there’s no need to call close()
explicitly.
Conclusion
This article explored how to handle files in Python, focusing on safe file reading, writing, and management strategies. By learning key concepts such as file modes, methods like read()
and write()
, and best practices like using the with()
statement, you can confidently read from files in Python, ensuring data integrity and proper resource management. With this knowledge, you can effectively interact with text files in Python while avoiding common pitfalls.
To further your understanding of Python file handling, including advanced techniques as well as best practices, you can explore Codecademy’s course on Python programming.
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Create and View a Web Page on Your Computer
If you've completed many Codecademy courses, but still find yourself asking, "Where can I write code on my own computer?", then start here! - Article
Create and View a Web Page on Your Computer
If you've completed many Codecademy courses, but still find yourself asking, "Where can I write code on my own computer?", then start here!
Learn more on Codecademy
- Skill path
Code Foundations
Start your programming journey with an introduction to the world of code and basic concepts.Includes 5 CoursesWith CertificateBeginner Friendly4 hours - Career path
Full-Stack Engineer
A full-stack engineer can get a project done from start to finish, back-end to front-end.Includes 51 CoursesWith Professional CertificationBeginner Friendly150 hours