withopen('abc.txt','r')asf:f.namef.mode# r (read)f.closed# Falsef.readable()# true or falsef.read(size=n)# after n characers seek to next characterf.readline(n)# return first line or atmost n bytes and moves # seek position to start of second linef.readlines(n)# returns a list of lines from the file. # Reads in at most n bytes/characters if specified.f.tell()# current seek positionf.seek(0)# change seek position. 0: start; 1: current; 2:end of the filef.writable()# true or falsef.write(s)# write string s and returns no of characters writtenf.writelines(lines)# write list of lines# f = open('abc.txt', 'r')# f.close()forlineinf:print(line,end='')# print each linewithopen('abcd.txt','w')asf:# w: overwrite; a: appendf.write('text')# if we f.write() again in same open block. text will be appended# copy from one file to otherwithopen('abc.txt','r')asrf:withopen('abcd.txt','w')aswf:forlineinrf:wf.write(line)# make a copy of image filewithopen('abc.jpg','rb')asrf:withopen('abcd.jpg','wb')aswf:forlineinrf:wf.write(line)
Here is a list of the different modes of opening a file:¶
r
Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rb
Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+
Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
rb+
Opens a file for both reading and writing in binary format. The file pointer will be 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. That is, the file is in the append mode. 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. That is, the file is in the append mode. 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. The file opens in the append mode. 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. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.