Python provides various inbuilt functions to read files in Python.
Here are general steps to read a file in Python.
- Open the file to get file object using open() method. You can specify different access modes while opening the file.
- Use the file object to perform read, write and append operations.
- Close the file object
Open and read a file in python
Here is content of text file sample.txt which we are going to read.
First line in the file
Second line in the file
Third line in the file
You can use in built function open() which returns a file object and you can call read() method on file object to read content of the file.
f = open("sample.txt", "r")
print(f.read())
open() function takes 2 arguments. One is filename and other is access mode.
open(“sample.txt”) opens the sample.txt file in read mode from current working directory and returns file object.
We have used f.read() method to read file into the String in Python.
Note: If file is not present in current directory, then you need to specify absolute path of the file.
Read line from the file
If you want to read file line by line you can use readline() method.
f = open("sample.txt", "r")
line1=f.readline()
print(line1)
line2=f.readline()
print(line2)
Output:
First line in the file
Second line in the file
Read all lines in file
You can use all the lines in file using readlines() method
f = open("sample.txt", "r")
lines=f.readlines()
print(lines)
Output:
First line in the file
Second line in the file
Third line in the file
Read all lines in file using for loop
You can read all lines in files using for loop as below:
fi = open("sample.txt", "r")
for line in fi:
print(line)
fi.close()
Output:
First line in the file
Second line in the file
Third line in the file
Read file in binary mode
You can use open() function with “rb” mode to read a file in binary mode.
Here is an example:
f = open("picture1.png", "rb")
print(f.read())
Output:
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00x\x00x\x00\x00\xff\xe1\x00,Exif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x01\x011\x00\x02\x00\x00\x00\n\x00\x00\x00\x1a\x00\x00\x00\x00Greenshot\x00\xff\xdb\x00C\x00\x07\x05\x05\x06\x05\x04\x07\x06\x05\x06\x08\x07\x07\x08\n\x11\x0b\n\t\t\n\x15\x0f\x10\x0c\x11\x18\x15\x1a\x19\x18\x15\x18\x17\x1b\x1e\'!\x1b\x1d%\x1d\x17\x18"."%()+,+\x1a /3/*2\'*+*\xff\xdb\x00C\x01\x07\x08\x08\n\t\n\x14\x0b\x0b\x14*\x1c\x18\x1c**************************************************\xff\xc0\x00\x11\x08\x01\xa7\x04 \x03\x01"\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07"q\x142\x81\x91\xa1\x08#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&\'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7
If you are reading file in bytes mode using access mode “rb” and reading it as String, then you will get TypeError: A Bytes-Like object Is Required, Not ‘str’
You should use decode() method to convert byte object back to string as follow:
with open(sample.txt', 'rb') as f:
lines = [x.decode('utf8').strip() for x in f.readlines()]
print(lines)
output:
b'First line in the file\r\n\r\nSecond line in the file\r\n\r\nThird line in the file'
That’s all about how to read a file in Python.