File handling is a fundamental aspect of programming, allowing developers to read, write, and manipulate files. Python, a versatile and powerful programming language, provides robust file handling capabilities that make it easy to work with various file formats. In this article, we will explore the different techniques and functions available in Python for file handling.
Opening files in Python
We can open files in Python by using the open()
method. We'll use SomeText.txt
as an example of how Python opens files.
To open a file, we use the following syntax:
Now, let's try to open data from this file using the open()
function.
# open file in current directory
file = open("SomeText.txt")
By default, the files are open in read mode. The code above is equivalent to
file = open("SomeText.txt", "r")
Closing Files in Python
After performing operations on the file, it's essential to close it using the close()
method to free up system resources.
file = open("SomeText.txt", "r")
# Perform operations on the file
file.close()
Automatic File Closure with with
Python offers a convenient way to automatically close files using the with statement. It ensures that the file is closed regardless of any exceptions or errors.
with open('filename', 'r') as file:
# Perform file operations
File Open Modes
Mode | Description |
---|---|
"r" | Reading only. The file pointer at the BOF |
"w" | Writing only. If file exists, it’ll be overwritten; If it doesn't a new file will be created for writing. |
"a" | Appending. The file pointer is at EOF if the file exists. If the file does not exist, it creates a new file for writing. |
"x" | Exclusive creation mode. Creates a new file but raises an error if the file already exists. |
"b" | Binary mode. Opens a file in binary mode for reading or writing. |
"t" | Text mode (default). Opens a file in text mode for reading or writing. |
The
"+"
mark can be used in combination with the "r", "a", and "w" modes to enable additional functionalities when opening a file in Python.
- BOF (Beginning of File): BOF refers to the starting position of a file. It indicates the first byte or character of the file. When reading a file, starting from the BOF means reading from the very beginning of the file. In some cases, when a file is opened in a specific mode (e.g., "r+", "w+"), the file pointer might initially be positioned at the BOF.
Read Mode ("r")
file = open("SomeText.txt", "r")
content = file.read()
print(content)
file.close()
- Using
with
with open("SomeText.txt", "r") as file:
content = file.read()
print(content)
In the read mode, the file is opened for reading. The read()
method is used to read the entire content of the file.
Output:
Write Mode ("w")
file = open("SomeText.txt", "w")
file.write("This is written by python Program.")
file.close()
- using
with
with open("SomeText", "w") as file:
file.write("This is written by python Program.")
In the write mode, the file is opened for writing. If the file doesn't exist, a new file is created. If the file already exists, its contents are truncated. The write() method is used to write data to the file.
Output:
Append Mode ("a")
file = open("SomeText.txt", "a")
file.write("This is append mode. Pointer is at EOF")
file.close()
Using with
with open("SomeText.txt", "a") as file:
file.write("This is append mode. Pointer is at EOF")
In the append mode, the file is opened for appending data. If the file doesn't exist, a new file is created. The write() method is used to append data to the file.
Exclusive Creation Mode ("x")
In the exclusive creation mode, the file is opened for writing, but only if the file doesn't already exist. If the file exists, a FileExistsError
is raised.
# SomeText.txt already exists
file = open("SomeText.txt", "x")
file.write("I am trying Exclusive Creation Mode.")
file.close()
Output
Trying in correct way
# give new file name for create it.
file = open("Create_SomeTextFile.txt","x")
file.write("I am trying Exclusive Creation Mode.")
file.close()
- Using
with
with open("Create_SomeTextFile.txt","x") as file:
file.write("I am trying Exclusive Creation Mode.")
Output
Binary Mode ("b")
In the binary mode, the file is opened for reading or writing binary data. This mode is commonly used for working with non-text files such as images, audio files, or binary data files.
Read .bin file
Click Here to download an example binary file and it moves to the python file directory or copy the file path and replace file name with the path.
file = open("example_binary_file.bin", "rb")
content = file.read()
print(content)
file.close()
using with
with open("example.bin", "rb") as file:
data = file.read()
If you're using file path, replace the first line with this
file = open(r"C:\Users\nuwan\Developer\SomeFileHandle\example_binary_file.bin", "rb")
and change your path.
Note: the "r" in front of the path allows you to specify the file path as a raw string literal, making it easier to work with Windows file paths without manually escaping backslashes.
Output
Read images
To read an image using the open() method in Python, you can open the image file in binary mode ("rb") and then use the appropriate image processing libraries to handle the image data. Here's an example of reading an image file using the open() method and the PIL (Python Imaging Library) library.
Note: For this example to work, you need to have the
PIL
library installed. You can install it usingpip
Copy this command and paste it in your command prompt or terminal.
pip install pillow
let's try,
from PIL import Image
# Open the image file in binary mode
with open("hello.png", "rb") as file:
# Create a PIL Image object from the binary data
image = Image.open(file)
# Perform operations on the image
# For example, you can display the image
image.show()
Output
Text Mode ("t")
In the text mode (default mode), the file is opened for reading or writing text data. This mode is used for handling plain text files.
Remember to replace "example.txt" or "example.bin" with the actual file path and name you want to work with.
file = open("SomeText.txt", 'rt')
content = file.read()
print(content)
file.close()
Using with
with open("SomeText.txt", "rt") as file:
content = file.read()
print(content)
These examples showcase the different modes available in the open()
method for file handling in Python. Choose the appropriate mode based on your requirements to read from or write to files effectively.
Reading from Files
Python offers several methods to read data from files
1. Reading the Entire File
To read the entire contents of a file, you can use the read()
method. It returns the contents of the file as a string.
file = open('filename', 'r')
content = file.read()
print(content)
file.close()
2. Reading Line by Line
If you want to read a file line by line, you can use a loop along with the readline()
method. It returns the next line in the file as a string.
file = open('filename', 'r')
for line in file:
print(line)
file.close()
Alternatively, you can use the readlines()
method to read all the lines of a file into a list.
file = open('filename', 'r')
lines = file.readlines()
for line in lines:
print(line)
file.close()
Writing to Files
1. Writing a String
To write a string to a file, you can use the write()
method. It takes a string as input and writes it to the file.
file = open('filename', 'w')
file.write('Hello, World!')
file.close()
2. Writing Multiple Lines
To write multiple lines to a file, you can pass a list of strings to the writelines()
method. Each string represents a line in the file.
file = open('filename', 'w')
lines = ['Line 1\n', 'Line 2\n', 'Line 3\n']
file.writelines(lines)
file.close()
File Seeking
File seeking is a crucial concept in file handling that allows you to navigate through a file and read or write data at specific positions.
The Seek Pointer
Before we delve into file seeking, let's understand the concept of the file seek pointer. Every open file in Python has a seek pointer that keeps track of the current position within the file. This pointer points to the next byte that will be read or written.
When you open a file, the seek pointer is positioned at the beginning of the file (offset 0). As you perform read or write operations, the pointer automatically advances. However, you can manually move the seek pointer to a specific position within the file using the seek() method.
Note: When you press the Enter key on a keyboard, two special characters are typically generated: the carriage return (
'\r'
) and the line feed ('\n'
).
-In many systems and programming languages, including Python, the combination of these two characters is used to represent a line break or the end of a line. The specific order in which these characters appear can vary depending on the operating system:
-
Unix-like systems (e.g., Linux, macOS): The standard newline sequence is
\n
(line feed character) alone. -
Windows systems: The standard newline sequence is
\r\n
(carriage return followed by line feed characters).
The seek() Method
The seek() method allows you to move the seek pointer to a desired position within the file. Its syntax is as follows:
file.seek(offset, whence)
Here, offset represents the number of bytes to move, and whence specifies the reference point from where the movement should occur. Python supports three values for whence:
0
(default): Moves relative to the beginning of the file.1
: Moves relative to the current position.2
: Moves relative to the end of the file.
To illustrate how seek() works, consider the following example:
First, create a text file like below,
And run this code snippet.
file = open('SomeText.txt', 'r')
file.seek(10, 0) # Moves 10 bytes from the beginning of the file
data = file.read(5) # Reads 5 bytes from the current position
print(data)
file.close()
In this example, we move the seek pointer 10 bytes from the beginning of the file and then read the next 5 bytes starting from that position. By manipulating the seek pointer, you can access specific sections of the file.
Output
Seeking in Different Modes
File seeking behavior varies depending on the mode in which the file is opened. Let's explore how seeking works in different modes:
-
Read Mode ('r'): In read mode, seeking allows you to move freely within the file and read data from any position. The seek pointer is advanced as you read data.
-
Write Mode ('w') and Append Mode ('a'): In write and append modes, seeking typically doesn't make sense since the primary purpose is to write data at the end of the file. Therefore, seeking operations are not commonly used in these modes.
-
Binary Mode ('b'): Binary mode allows you to perform seek operations using offsets in bytes. It provides more granular control over file navigation, especially when dealing with binary data.
The tell() Method
The tell()
method is used to determine the current position of the seek pointer within the file. It returns the current offset from the beginning of the file in bytes. The syntax is as follows:
position = file.tell()
Summery of Python File Methods
File objects can be accessed through a variety of methods. The above examples have used a few of them.
Below is a list of all methods in text mode, with a brief description of each.
Method | Description |
---|---|
close() | Closes an opened file. It has no effect if the file is already closed. |
detach() | Separates the underlying binary buffer from the TextIOBase and returns it. |
fileno() | Returns an integer number (file descriptor) of the file. |
flush() | Flushes the write buffer of the file stream. |
isatty() | Returns True if the file stream is interactive. |
read(n) | Reads at most n characters from the file. Reads till end of file if it is negative or None . |
readable() | Returns True if the file stream can be read from. |
readline(n=-1) | Reads and returns one line from the file. Reads in at most n bytes if specified. |
readlines(n=-1) | Reads and returns a list of lines from the file. Reads in at most n bytes/characters if specified. |
seek(offset,from=SEEK_SET) | Changes the file position to offset bytes, in reference to from (start, current, end). |
seekable() | Returns True if the file stream supports random access. |
tell() | Returns an integer that represents the current position of the file's object. |
truncate(size=None) | Resizes the file stream to size bytes. If size is not specified, resizes to current location. |
writable() | Returns True if the file stream can be written to. |
write(s) | Writes the string s to the file and returns the number of characters written. |
writelines(lines) | Writes a list of lines to the file. |
File handling is a fundamental skill in Python programming. Understanding how to open, read, and write files is essential for many real-world applications. In this lesson, we explored the different methods available for file handling in Python. By mastering these techniques, you'll have the necessary tools to manipulate files effectively using Python.