Read, Write and Append in a file using Python
Reading & Writing in files
The user must open a file first using open() to read it.The reading of string always starts with beginning of the file.
Methods to perform read operation
read() ,readline() & readlines()
read()
: This method is used to read entire size of a file.
Syntax :
obj.read(count) # if count is not specified it will read entire file
readline() :
This method is used to read single line at a time.
Syntax :
obj.readline()
readlines() :
This method is used to read all lines of a file.It returns list of lines from the stream.
Syntax :
obj.readlines()
Example :
""" Author : ITVoyagers (https://itvoyagers.in/) Date : 19th December 2018 Description : Program to use of read(),readline() and readlines() """ #create file object and open file in read mode
with open("itvoyagers.txt") as iv: c=iv.read() print(c) with open("itvoyagers.txt") as iv: c=iv.readline() print(c) with open("itvoyagers.txt") as iv: c=iv.readlines() print(c)
(Note : In above example file mode is not mentioned as by default it is in read mode.Also we can use any method to open a file and create a file object like iv=open(“itvoyagers.txt”)
The user must open a file first using open() to write in a file.The writing of string always starts with beginning of the file.
Methods to perform write operation
write() and writelines()
write()
: This method is used to write single line at a time in a file.
Syntax :
obj.write("string")
writelines() :
This method is used to write multiple lines at a time in a file.It takes list of lines as input and writes them in file
Syntax :
obj.writelines([list]) #we can also pass tuple
Example :
""" Author : ITVoyagers (https://itvoyagers.in/) Date : 19th December 2018 Description : Program to use of write() and writelines() """ #create file object and open file in write mode object=open("itvoyagers.txt",'w') object.write("first statement n") object.write("second statement n") bulk=("This is Itvoyagers blog","n this is python practical","n this is to check use of writelines function") object.writelines(bulk) object=open("itvoyagers.txt",'r') c=object.read() print(c)
(Note : In above example bulk is a variable name of tuple type instead you can also use a list)
Appending text to a file
To understand above concept , we must first know difference between appending and writing into files.Once the file is created we can use write and append mode to edit in that file, but write mode will overwrite existing data with new data whereas append mode will start adding new data from the end of the existing file if data is available else it will start from beginning of the file.
While writing code for append mode for text files ‘a’,’a+’ and for binary files ‘ab’,’ab+’ is added in open(),but to write strings in that file write() will be used.
Syntax :
object=open("filename.txt",'a')
Example :
""" Author : ITVoyagers (https://itvoyagers.in/) Date : 23rd December 2018 Description : Program to use of append mode and to show difference in write and append modes """ #create file object and open file in append mode object=open("itvoyagers.txt",'w') #creates file and with write mode and added strings object.write("first statement n") object.write("second statement n") """if we use same object to open another file or same file in different mode we do not need to use object.close()""" object=open("itvoyagers.txt",'r') c=object.read() print("statements written using write mode n",c) object=open("itvoyagers.txt",'a') #added more strings without overwriting using append mode object.write("third statement n") object=open("itvoyagers.txt",'r') c=object.read() print("statements written using append mode n",c) object=open("itvoyagers.txt",'w') #added strings by overwriting using write mode object.write("fourth statement n") object=open("itvoyagers.txt",'r') c=object.read() print("statements written using write mode n",c)
Note : There is no method called append() for appending text to file we have to use write() using append mode
You can also check other file related operations and modes
- Remaining file methods in Python
- File Positions in Python
- Types of file modes and attributes of file object in Python
- File Systems and File Handling in Python