PHY 3032 Chapter 10A - File Handling in Python 3
PHY 3032 Chapter 10A - File Handling in Python 3
File Handling
File handling is an important part of computer programming. Python programs, often have to read information
that was saved in files. Python has several functions for creating , reading , updating , and
deleting files.
Learning Objectives
By the end of this lesson, you will learn how to
The general recipe for opening a file with open() is given below:
The filename argument is a string value that contains the name of the file that you want to access.
The mode determines the mode in which the file has to be opened. This is optional parameter and the
default file access mode is read i.e. r .
The file object which would be utilized to call other support methods associated with it.
r : Opens a file for reading only . If the file does not exit, you get an error. The file pointer is placed
at the beginning of the file.
rb : Opens a file for reading only in binary format . The file pointer is placed at the
beginning of the file.
r+ : Opens a file for both reading and writing . The file pointer placed at the beginning of the
file.
rb+ : Opens a file for both reading and writing in binary format . The file pointer placed
at the beginning of the file.
w : Opens a file for writing only . Overwrites the file if the file exists. If the file does not exist,
creates a new file for writing.
wb : Opens a file for writing only in binary format . Overwrites the file if the file exists. If the
file does not exist, creates a new file for writing.
w+ : Opens a file for both writing and reading . Overwrites the existing file if the file exists. If the
file does not exist, creates a new file for reading and writing.
wb+ : Opens a file for both writing and reading in binary format . Overwrites the existing
file if the file exists. If the file does not exist, creates a new file for reading and writing.
a : Opens a file for appending . The file pointer is at the end of the file if the file exists. If the file does
not exist, it creates a new file for writing.
ab : Opens a file for appending in binary format . The file pointer is at the end of the file if the
file exists. If the file does not exist, it creates a new file for writing.
a+ : Opens a file for both appending and reading . The file pointer is at the end of the file if the
file exists. If the file does not exist, it creates a new file for reading and writing.
ab+ : Opens a file for both appending and reading in binary format . The file pointer is at
the end of the file if the file exists. If the file does not exist, it creates a new file for reading and writing.
To open a file for reading, it is enough to specify the name of the file.
In [1]:
file = open("black_sheep.txt")
You can also open a file for reading by specify the name of the file and file access mode:
In [2]:
file = open("black_sheep.txt","r")
In [3]:
In [4]:
In [5]:
The general recipe for closing a file with the close() method is given below:
In [6]:
file.close()
The general recipe for reading an entire file with read() method is given below:
The read() method starts reading the file from the beginning, then tries to read as much as possible,
maybe until the end of file.
In [7]:
file = open("black_sheep.txt","r")
In [8]:
print(file.read())
In [9]:
file.close ()
The general recipe for reading only a part of a file with read() method is given below:
Where the passed optional parameter count is the number of bytes to be read from the opened file.
In [10]:
In [11]:
print(file.read (10))
Baa, baa,
In [12]:
file.close()
In [13]:
file = open("black_sheep.txt","r")
In [14]:
print(file.readline())
In [15]:
print(file.readline())
In [16]:
print(file.readline())
print(file.readline())
In [17]:
print(file.readline())
print(file.readline())
print(file.readline())
print(file.readline())
In [18]:
file.close()
By looping through the lines of the file, you can read the whole file, line by line:
In [19]:
# close file
file.close()
In [20]:
file = open("black_sheep.txt","r")
In [21]:
print(file.readlines())
['Baa, baa, brown sheep,\n', 'Have you any wool?\n', 'Yes sir, yes
sir,\n', 'Three bags full.\n', 'One for the master,\n', 'One for th
e dame,\n', 'And one for the Old Man,\n', 'Who lives down the lan
e.\n', '\n']
In [22]:
file.close()
Since the readlines() method returns a list, you can loop through the list items using a for loop. By
looping through the lines of the file, you can read the whole file, line by line.
In [23]:
# open a file
file = open("black_sheep.txt","r")
# close file
file.close()
In [24]:
In [25]:
Out[25]:
25
In [26]:
file.close()
In [27]:
# read files
print(file.read())
# close file
file.close()
In [28]:
In [29]:
Out[29]:
25
In [30]:
file.close()
In [31]:
# read files
print(file.read())
# close file
file.close()
In [34]:
In [35]:
file = open("twinkle.txt","r")
In [36]:
print(file.read())
In [37]:
file.close()
In [38]:
In [39]:
Out[39]:
25
In [40]:
file.close()
In [41]:
# close file
file.close()
In [42]:
filename = "nobel_laureates.csv"
In [43]:
In [44]:
Out[44]:
40
In [45]:
file.close()
In [46]:
# read file
print(file.read())
# close file
file.close()
In [47]:
In [48]:
print(cars)
In [49]:
print(country)
In [50]:
filename = "cars.csv"
Check if the sizes of lists "cars" and "country" are the same.
In [51]:
len(cars) == len(country)
Out[51]:
True
In [52]:
list_size = len(cars)
In [53]:
file = open(filename,"w")
In [54]:
In [55]:
file.close()
In [56]:
# read file
print(file.read())
# close file
file.close()
bmw , germany
audi , germany
toyota , japan
subaru , japan
mazda , japan
jeep , usa
mercedes benz , germany
fiat , italy
kia , south korea
mahindra , india
ford , usa
byd , china
The seek(offset[, from]) method changes the current file position. The offset argument
indicates the number of bytes to be moved. The optional from argument specifies the reference position
from where the bytes are to be moved. If from is set to 0 , it means use the beginning of the file as the
reference position. If from is set to 1 , it means use the current position as the reference position and if it
is set to 2 then the end of the file would be taken as the reference position.
In [57]:
Read the first 10 bytes of text in "twinkle.txt" and store then in variable text .
In [58]:
text = file.read(10)
In [59]:
In [60]:
position = file.tell()
In [61]:
In [62]:
Read the first 50 bytes of text in "twinkle.txt" and store then in variable text .
In [63]:
text = file.read(50)
In [64]:
In [65]:
file.close()