Python Program for Read and Write Files

open method:

Predefined open() function is used to open a file and gets a file object.
File object contains the methods and properties that can be used to get information about the opened file.
file_object  = open(“file_name”, “mode”)
There are two arguments in open method, file name is the file path and mode argument is used to restrict the access in the opened file.
mode argument default value is 'r' which is read mode access and mode is optional argument for open method.
different mode values are:
  • 'r’ is for read mode which is used to open the file for only reading.
  • ‘w’ is for write mode which is used to open the file to edit and write the information.
  • ‘a’ is appending mode which is used to append the information to the end of the file.
  • ‘r+’ is special read and write mode which is used to support both reading and writing in the opened file .
  • 'rb' is opening a file to read only in binary format.
  • 'wb' is opening a file to write in binary format. Overwrites if file already available in the path.
  • 'rb+' is opening a file for both reading and writing in binary format.
  • 'wb+' is opening a file for both writing and reading in binary format. Overwrites if file is already available in path.
f = open(“file_name”, ”w”) 
Print f 

Writing New File:

Write method in file object is used to add text in a file.
f = open(“file_name.txt”, ”w”) 
f.write(“Welcome!”) 
f.write(“Test file.”) 
f.close() 

Reading File:

Read method in file object is used to get the opened file content as string.
f = open(“file_name.txt”, “r”) 
print f.read()
f.close() 

Number of characters required in file content can be provided in read method.
f = open(“file_name.text”, “r”)
print f.read(10) # prints first 10 characters in file content
f.close() 

readline method:


reads the first line in the file content.
f = open(“file_name.txt”, “r”) 
print f.readline():  # also specify which line to be read like f.readline(3): 
f.close()

Looping Over File Object:

f = open(“file_name.txt”, “r”) 
#prints all the lines from the file content.
for line in f:   
    print line
f.close()

Writing list of lines in a file:

writelines method is used to write array of strings in a file.
lines = [“first line”, “second line”, “last line”] 
f = open(“file_name.txt”, ”w”) 
f.writelines(lines) 
f.close() 

with statement:

with statement defines the scope for the file object and closes automatically in end of the scope.
use 'with statement' is the best practice when opening any file to read or write.
with open(“file_name.txt”) as f: 
    for line in f: 
        print line

Reading Binary File:


with open("file_name.bin", "rb") as f:
    byte_code = f.read(1)
    while byte_code != "":
        byte = f.read(1)

Writing Binary File:

with open(file_name.bin, 'br+') as f:
    f.write(b'\x07\x08\x07') #can also specify bytes array.

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^