New Batch Starts on 16th Oct.
Module: Network Fundamentals (L2 L3 Protocol Testing)
Timing: 07:00 AM to 08:00 AM IST (Weekdays)
Sign Up Now !

Chapter 11: Python File Handling -Reading, Writing, and Managing Files

Working with File Operations in Python

In this chapter, we’ll explore how to work with files in Python, including reading, writing, and managing files efficiently. Files are often used to store inputs or outputs during script execution, and Python’s built-in support for file handling makes this process easy. We’ll go over the most commonly used file handling methods like open(), read(), write(), and seek(). You’ll also learn about file modes, binary files, and how Python manages buffering when handling large amounts of data. By the end of this chapter, you’ll be equipped to handle text and binary files in Python with confidence.

Commonly Used File Handling Functions :

File FunctionDescription
open()Used to create or open a file.
fid.read()Reads the entire content or specific bytes.
fid.readline()Reads a file line by line.
fid.write()Writes data to a file.
fid.writelines()Writes multiple lines to a file.
fid.seek()Moves the file object to a specific position.
fid.tell()Returns the current position of the file object.
fid.readable()Checks if the file is readable.
fid.writable()Checks if the file is writable.

Opening a File Using open()

The open() function is used to open a file and returns a file object. This file object can be used to read, write, or manipulate the file using methods like read(), write(), and close().

Syntax:

The first argument is the file name. The second argument ("r") is the mode, indicating how the file should be opened.

File Modes:

ModeDescription
rOpens the file for reading. The file must exist.
r+Opens the file for both reading and writing. The file must exist.
wOpens the file for writing. If the file doesn’t exist, it will be created.
w+Opens the file for reading and writing. If the file doesn’t exist, it is created.
aOpens the file for appending. If the file doesn’t exist, it is created.
a+Opens the file for both appending and reading
xCreates a new file for writing. Raises an exception if the file exists.

Reading from a File

We can read the contents of a file in a couple of ways. Make sure the file is present before using the ‘read’ mode. If the file is not present, an ‘OSError’ will be raised.Before trying the following code, create a file named ‘abcd.txt’ with the following lines in the same folder where you are executing your script.

File name : abcd. txt

Method 1. Using read() : The read() function reads the entire content of a file as a string.

Explanation:
The read() function reads the entire file and returns it as a string. You can also specify a number of bytes to read.

Method 2: Using readline()

Explanation: The readline() function reads one line at a time from the file. This is useful when you want to process files line by line.

Method 3: Using readlines()

Explanation:
The readlines() function reads the entire file and returns it as a list where each element is a line from the file.

Writing to a File Using write()

The write() function is used to write data to a file. If the file is opened in "w" mode, any existing content will be deleted before writing new content.

Explanation:
This code opens the file abcd.txt in write mode ("w"), writes the string "hello world", and then closes the file. Writing in "w" mode overwrites the existing content.

Appending to a File Using writelines()

The writelines() function writes a list of lines to a file without adding newlines unless specified in the data.

Explanation:
This code writes multiple lines to the file xyz.txt using the writelines() method.

Moving the Cursor with seek() and Getting the Position with tell()

Using seek(): The seek() function moves the cursor to a specified position in the file. The first argument is the number of bytes, and the second argument is the reference position (0 for the start, 1 for the current position, and 2 for the end).

Using tell(): The tell() function returns the current position of the cursor.

Explanation:
This method is useful for tracking where the cursor is located in the file during reading or writing operations.

Differences Between r+ and w+ Modes

r+ allows both reading and writing without overwriting the content. If you read the file first, the cursor moves to the end, and any writing will append at the end.

w+ allows reading and writing, but it first clears the file content, so there’s nothing to read unless you write something first.

Text Files vs Binary Files

Text Files: Store data in the form of text strings with specific encoding like UTF-8 or ASCII. End of line (EOL) characters vary by platform (\r\n for Windows, \n for Linux).

Binary Files: Store data like images, audio, or executables. No encoding or EOL conversion is performed by Python for binary files. For binary operations, use modes like "rb", "wb", "ab".

Buffering in File Operations

When reading or writing to a file, Python stores data in a buffer to improve performance. The buffer is a temporary storage that holds chunks of data, making operations faster.

Explanation:
The default buffer size is 8192 bytes. You can control buffering with the buffering parameter in open(). Set buffering=0 to disable buffering, buffering=1 for line buffering, or provide a specific buffer size in bytes.

Some Additional File Attributes:

  • fid.encoding: Returns the file encoding (e.g., ‘UTF-8’).
  • fid.mode: Shows the mode in which the file is opened (e.g., ‘r’).
  • fid.readable(): Checks if the file is readable.
  • fid.writable(): Checks if the file is writable.
  • fid.seekable(): Checks if the file supports seeking.
  • fid.name: Returns the file name.

We’d love to hear your feedback and suggestions about this article. Feel free to reach out to us using the WhatsApp number below.

Sajith Achipra has been a trainer and testing consultant at Zframez Technologies since 2009. With 15+ years of experience, he specializes in networking, Python, development, and testing. He conducts online courses to help students and professionals enhance their skills. You can reach him on WhatsApp at +91 8884 884 844 for your training and testing requirements.