Dr. Harpreet Singh
Head, PG Department of Bioinformatics,
Hans Raj Mahila Maha Vidyalaya,
Jalandhar, Punjab, India
e-module
File Handling in Python: Reading/Writing data to/from files
Methods of input data
Why Reading from files?
5. Makes the program more flexible.
6. Mostly, in programming languages, all the values or data are stored in some variables which are volatile in nature.
7. Because data will be stored into those variables during run-time only and will be lost once the program execution is completed.
Types Of File in Python�
They are:�
Binary Files
Most of the files that we see in our computer system are called binary files.
Example:
For Example, You need Microsoft word software to open .doc binary files. Likewise, you need a pdf reader software to open .pdf binary files and you need a photo editor software to read the image files and so on.
Text Files�
Text files don’t have any specific encoding and it can be opened in normal text editor itself.
Example:
Reading a file is a three-step process in Python:
Step1: Open the file
name of the file and opening mode
The file name is a string with the file name, in most cases including the system path. When the system path is included, this absolute path is used by the program. In case you enter just the file name (without any path), a relative path is assumed
Step1: Open the file
“r” to read
“w” to write
“a” to append data at the end of a file.
“r+” to Read or Write
“a+” to Append or Read
Note: The above-mentioned modes are for opening, reading or writing text files only.
While using binary files, we have to use the same modes with the letter ‘b’ at the end. So that Python can understand that we are interacting with binary files.
For example “wb” – Open a file for write only mode in the binary format.
Step1: Open the file
Using open create a file handle to read a file:
Example 1:
>>> file_handle = open(’readme.txt’, ’r’)
In the above example, we are opening the file named ‘’readme.txt’, present at the current location.
Example 2:
>>> file_handle = open(“C:/Documents/Python/test.txt”, “r+”)
In the above example, we are opening the file named ‘test.txt’ present at the location ‘C:/Documents/Python/’ and we are opening the same file in a read-write mode which gives us more flexibility.
Example 3:
>>> file_handle = open(“C:/Documents/Python/img.bmp”, “rb+”)
In the above example, we are opening the file named ‘img.bmp’ present at the location “C:/Documents/Python/”, But, here we are trying to open the binary file.
Note: - file_handle is not the file, but a reference to it.
Step2: Reading the file
Once the file is opened, we can read its contents. The file handle has several methods to read a file; here are the most used:
Step2: Reading the file: Sample File
Now let’s observe what each read method does:
Let’s create a sample text file named test1.txt as shown below.
Step2: Reading the file: Examples
>>> my_file = open(“D:/test1.txt”, “r”)
>>> print(my_file.read(5))
Output:
>>> Hello
Here we are opening the file test.txt in a read-only mode and are reading only the first 5 characters of the file using the my_file.read(5) method.
Example 1:
Step2: Reading the file: Examples
>>> my_file = open(“D:/test1.txt”, “r”)
>>> print(my_file.read())
Output:
Hello World
Hello Bioinformatics
Hello Python
Here we have not provided any argument inside the read() function. Hence it will read all the content present inside the file.
Example 2:
Step2: Reading the file: Examples
>>> my_file = open(“D:/test1.txt”, “r”)
>>> print(my_file.readline())
Output:
Hello World
Using this function we can read the content of the file on a line by line basis.
Example 3:
Step2: Reading the file: Examples
>>> my_file = open(“D:/test1.txt”, “r”)
>>> print(my_file.readlines())
Output:
['Hello World\n', 'Hello Bioinformatics\n', 'Hello Python']
Here we are reading all the lines present inside the text file including the newline characters.
Example 4:
Step2: Reading the file: Examples
>>> filename = “C:/Documents/Python/test.txt”
>>> filehandle = open(filename, ‘r’)
>>> filedata = filehandle.read()
>>> print(filedata)
Output:
Hello World
Hello Bioinformatics
Hello Python
Here we are reading all the lines present inside the text file including the newline characters.
Example 5: Reading the entire file at once
Writing to the file
We have two methods for writing data into a file as shown below.�
Writing to the file
Example 1:
>>> my_file = open(“D:/test.txt”, “w”)
>>> my_file.write(“Hello World”)
The above code writes the String ‘Hello World’ into the ‘test.txt’ file.
�
Writing to the file
Example 2:
>>> my_file = open(“D:/test.txt”, “w”)
>>> my_file.write(“Hello World\n”)
>>> my_file.write(“Hello Python”)
�
Writing to the file
Example 3:
>>> fruits = [“Apple\n”, “Orange\n”, “Grapes\n”, “Watermelon”]
>>> my_file = open(“D:/test.txt”, “w”)
>>> my_file.writelines(fruits)
The above code writes a list of data into the ‘test.txt’ file simultaneously.�
Writing to the file: Append Mode
Example 4:
>>> my_file = open(“D:/test.txt”, “a+”)
>>> my_file.write (“Strawberry”)
>>> my_file.write (“\nGuava”)
The above code writes a list of data into the ‘test.txt’ file simultaneously
To append data into a file we must open the file in ‘a+’ mode so that we will have access to both the append as well as read modes.
Step3: Closing the file
Example:
>>> my_file = open(“D:/test1.txt”, “r”)
>>> print(my_file.read())
>>> my_file.close()
References
Thanks