Files in Python
Files in Python
a To append data to the file. Appending means adding at the end of the existing data.
The file pointer is placed at the end of the file. If the file does not exist, it will create a
new file for writing data
w+ To write and read data of a file. The previous data in the file will be deleted
r+ To read and write data into file. The previous data in the file will not be deleted. The
file pointer is placed at the beginning of the file
a+ To append and read data of a file. The file pointer will be 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
x To open the file in exclusive creation mode. The file creation fails if the file already
exists
File opening mode for binary file
File open mode Description
wb To write data into file. If any data is already present in the file, it would be deleted
and the present data will be stored
rb To read data from the file. The file pointer is positioned at the beginning of the file
ab To append data to the file. Appending means adding at the end of the existing data.
The file pointer is placed at the end of the file. If the file does not exist, it will create a
new file for writing data
w+b To write and read data of a file. The previous data in the file will be deleted
r+b To read and write data into file. The previous data in the file will not be deleted. The
file pointer is placed at the beginning of the file
a+b To append and read data of a file. The file pointer will be 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
xb To open the file in exclusive creation mode. The file creation fails if the file already
exists
Closing a File
A file which is opened should be closed using the close() method. Once
the file is opened but not closed then the data of the file may be
corrupted or deleted in some cases.
Syntax for closing a file
filehandler.close()
filehandler i.e file object is being deleted means it is removed from
memory.
read() and write() functions
Once the file is opened we can perform read operation on a file using read()
function.
Syntax
f=open(“mydata”,”r”)
str=f.read()
print(str)
f.close()
read() reads all lines from the file pointed by file object f and stores it in string
object str
Python program to read the content of a file mydata
write() function
To perform write operation on a file write() is used
Syntax:
s=open(“s.data”, “w”)
s.write(“hello”)
p=“how are you”
s.write(p)
s.close()
Python program to read a line of text from keyboard and write it to a
file inputted by the user.
readline(),readlines()
Reads a line from the text file assumes that newline character as
indicator for end of line
Python program to read each line from a file biodata and count number
of lines using readline() function
Python program to read each line from a file biodata and count number
of lines using readlines() function
Python program to read each line from a file biodata and count number
of lines by iterating over the file object
continued
Python program to accept string from user until user press @ symbol.
Write the inputted string to a file input by the user
Python program to read a lines from text file that starts with from and
print it on screen
continued
Python program that generates the factorial of all integers from 1 to 10
and stores them in a file called fact.txt
Python program to read the numbers stored in a file test.txt. Check the
number is even or odd. If even write it to a file even.txt otherwise write
it to a file odd.txt
Binary file
Python program to copy an image file into another file