File Handling in Python: 18103097 Parijat Garg 18103122 Atul Kumar
File Handling in Python: 18103097 Parijat Garg 18103122 Atul Kumar
File Handling in Python: 18103097 Parijat Garg 18103122 Atul Kumar
Python
18103097 Parijat Garg
18103122 Atul Kumar
Modes Description
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.
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.
Networked API
Lorem ipsum dolor sit
amet, consectetuer
adipiscing elit
Example :
fo = open("foo.txt", "r")
print(f.readline())
print(f.readline(5))
fo.close()
Example :
fo = open("foo.txt", "r")
print(f.readlines(10))
fo.close()
writelines()
For a list of string elements, each string is inserted in the text
file.Used to insert multiple strings at a single time.
Syntax:
File_object.writelines(L) for L = [str1, str2, str3]
Your Logo or Name Here
file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris\n","This is London \n"] This would produce following result:
file1.writelines(L)
file1.close()
Output of Readlines after appending
file1 = open("myfile.txt","a")#append mode ['This is Delhi \n', 'This is Paris \n', 'This is
file1.write("Today \n") London \n', 'Today \n']
file1.close()
file1 = open("myfile.txt","r")
Output of Readlines after writing
print ("Output of Readlines after appending“) ['Tomorrow \n']
print (file1.readlines())
print()
file1.close()
file1 = open("myfile.txt","r")
print ("Output of Readlines after writing“)
print( file1.readlines() )
print() Your Logo or Name Here
file1.close()
File Output
• File objects have additional built-in methods. Say I have the file
object f:
• f.tell()gives current position in the file.
• f.seek(offset[, from]) offsets the position by offset
bytes from from position.
• In text files, only seeks relative to the beginning of the file are
allowed except seek(0, 2) and the only valid offset values are
those returned from the f.tell(), or zero
Attribute Description
file.closed Returns true if file is closed, false otherwise.