Python
Lab12: Text File Handling with Python
Python
Lab12: Text File Handling with Python
Read Mode Functions
To open a file in read mode, we need to use the following function:
open("filename", "r")
function opens a file in read mode. Both the file path and the mode need to be mentioned in the arguments.
myfile = open( 'RSTForum.txt', "r" )
To read the content of the opened file, we need to use the read() function. Following are different read functions:
read()
function reads the entire fileseek(0)
function sets the cursor to a particular position it starts to read from, here it starts reading from 0th characterread(5)
function reads 5 characters from the contentreadline()
function reads one line of the contentreadlines()
function reads all the lines in the content and returns a list of all the lines.
myfile.read() myfile.seek(0) myfile.read(5) myfile.readline() myfile.readlines()
'RST Forum\nThis is line 2\nThis is line 3\nThis is line 4\nThis is the last line '
0
'RST F'
'RST Forum\n'
['RST Forum\n', 'This is line 2\n', 'This is line 3\n', 'This is line 4\n', 'This is the last line ']
To save and close the file content using python, we use the following function:
close()
function saves the content in the text file and releases content of the file and the variable pointing to it from the memory location.
myfile.close()
Write Mode Functions
To open a file in write mode, we use the following function:
-
open("filename", 'w')
function creates a new file and opens it in write mode. We cannot read the content of the file in write mode. We can only write data in write mode. If the file exists, it will overwrite the file and existing data will be lost.
myfile = open( 'vendors.txt', 'w' )
To write data in the new text file, we use the following functions:
write()
function writes one line in the file.writelines()
function writes multiple lines in the file and needs to be passed a list of lines.read()
function is not supported in write modeclose()
function saves the content in the new text file and releases content of the file and the variable pointing to it from the memory location.
myfile.write('Apple \n') myfile.writelines(['Dell\n', 'MSI\n']) myfile.read() myfile.close()
Apple
Dell
MSI
Traceback (most recent call last):
File "", line 1, in module <io.UnsupportedOperation>: not readable
Append Mode Functions
To open a file in the append mode, the following function is used:
open("filename", 'a')
function opens an existing file and can perform write operations in the existing file. We cannot read the contents of the existing file in this mode. We can only add new data in the file. Append mode does not overwrite the file and existing data will not be lost.
myfile = open( 'vendors.txt', 'a' )
To write data in the existing file, the following functions are used
write()
function appends data at the last line of the filewritelines()
function appends multiple lines at the end of the fileseek(0)
function sets the cursor to the 0th character of the fileread()
function is not supported in append modeclose()
function saves the content in the existing text file and releases content of the file and the variable pointing to it from the memory location.
myfile.write('Lenovo \n') myfile.writelines(['Microsoft\n', 'HP\n', 'Toshiba\n'] myfile.seek(0) myfile.read(5) myfile.close()
8
0
Traceback (most recent call last):
File "", line 1, in <module>
io.UnsupportedOperation: not readable
Write Plus Mode Functions
To open a file in write-plus mode, the following function is used
open("filename", 'w+')
opens a new file in write-plus mode. We can read the content of the file in write-plus mode. If the file exists, it will overwrite the file and existing data will be lost.
myfile = open( 'vendors.txt', "w+" )
To use write-plus functions, the following functions are used
write()
function writes one line in the new fileseek(0)
function sets the cursor to the 0th character of the fileread()
function reads the entire file and is supported in write-plus mode
myfile.write('Razer Blade \n') myfile.seek(0) myfile.read()
13
0
'Razer Blade \n'
Append Plus Mode Functions
To open a file in the append-plus mode, the following function is used
open("filename", 'a+')
function opens an existing file and can perform write operations in the existing file. We can read the contents of the existing file in this mode. Append mode does not overwrite the file and existing data will not be lost.
myfile = open( 'vendors.txt', 'a+' )
To write data in the existing file, the following functions are used
write()
function appends one line at the end of the fileseek(0)
function sets the cursor to 0th positionread()
function reads the entire file
myfile.write('Amazon \n') myfile.seek(0) myfile.read()
Another way of opening a file:
with
statement is used in python to make code more readable and shorter- All the commands are indented within the with statement
- We don’t need to close the file explicitly by using with statement, it closes automatically after coming out of the identation.
with open('vendors.txt', "a+" ) as myfile: myfile.write('Acer \n') myfile.write('Asus \n')
8
0
'Razer Blade \nAmazon \n'
6
6