Python Read and Write File

In this tutorial, you’ll learn how to read and write file in Python.

Program to Write to File

f = open("newtext.txt","w")
f.write("something to be write")
f.close()

Output:

this program will create a new file (if not exist) named as “newtext.txt” in the same directory where our program is located and write a line “something to be write”.

Program to Read File

f = open("newtext.txt","r")
print f.read()
f.close()

Output:

something to be write

As we have seen the program, it is very easy to read and write into a file in python. In python we can work with two type of files Text files and Binary files. In this tutorial we are working with text file.

Python Read and Write File

Let’s understand above programs.

f = open("newtext.txt", "w")

open() is just a function to get the file object. It gets the file object and returns it to the f, we can name f as we want. using file object’s methods and attributes we can access the information that file have.

open() takes two arguments, first the file name (in our case, its “newtext.txt”) and second one is for access mode (optional).

Access Modes: it is an attribute of file object. Which tells, in which mode the file is opened.

There are 4 types of access modes.

  1. r: r stands for reading mode. If we open our file in this mode then we can only read the file’s information.
  2. w: w stands for writing mode. If we open our file in this mode then we can only write into the file, can’t read. If the file is not already exist then it will create new one, if it exists then it will delete all the previous content from the file and start writing from starting of the file.
  3. a: a stands for appending mode. It is same as writing mode but the difference is it will not delete the previous content of the file. It starts writing from the last of the file. In other words, it will append (concatenate) the new content with previous one.
  4. r+: special read and write mode. If we open the file using this mode then we can both read and write into the file.

Note: access mode is optional in open() method. By default the file will open into read mode (r).

Come to the next line of our program.

f.write("something to be write")

write() is a method of file object to write into the file. We will write in the parenthesis, what we want to write into the file (as shown above).

To read from the file:

print f.read()

read() is a method of file object to read the information stored in a file object. read() method read all the information from the given file object and returns it.

Last line in our program:

f.close()

close() is a method of file object to close the opened file. Which means we can’t access the file in our program now.

There are many other useful methods of the file objects like:-

read(int): if we pass any digit to the read() method then it will return same number of characters from the file object.

Example:

Let’s say we have a text file named as “newtext.txt” and “the crazy programmer” have written into it.

f = open("newtext.txt", "r")
print f.read(9)
f.close()

Output:

the crazy

readline(): it is used to read a single line (separated by enter key or \n ) from the file object.

Example:

Lets say our text file is look like this:

line number 1
line number 2
line number 3

f = open("newtext.txt", "r")
print f.readline()
f.close()

Output:

line number 1

f = open("newtext.txt", "r")
print f.readline()
print f.readline()
f.close()

Output:

line number 1
line number 2

readlines(): it reads all lines in the file object and return it as a List of lines.

Example:

f = open("newtext.txt", "r")
print f.readlines()
f.close()

Output:

[ ‘line number 1\n’ , ‘line number 2\n’ , ‘line number 3\n’]

If we want to print a specific line then:

f = open("newtext.txt", "r")
lines = f.readlines()
#to print the 3rd line 
print lines[2]
f.close()

Output:

line number 3

seek(int): it sets the position of reading and writing pointer. Using seek() method we can navigate to start or last of the file object.

This method takes 0,1,2 as argument.

  • 0 – takes pointer to the starting
  • 1 – current position of the pointer
  • 2 – takes pointer to the end

Example:

f = open("newtext.txt", "r")
print f.readline()
print f.readline()
f.seek(0)
print f.readline()
f.close()

Output:

line number 1
line number 2
line number 1

We can also use with statement to open the files for better indentation.

#to write into the file
with open("file.txt","w") as f:
f.write("example line")

It will create a new file named as “file.txt” and write “example line” into it.

Comment below if you have queries regarding above python file handling tutorial.

1 thought on “Python Read and Write File”

Leave a Comment

Your email address will not be published. Required fields are marked *