1. Home
  2. Docs
  3. Python 101 – Python Programming for Beginners
  4. Python File I/O: Read and Write Files in Python

Python File I/O: Read and Write Files in Python

File Handling in Python


In this section you learn about python file input and output operations.

What you can do with a file ?

You can carry out multiple file operations with file, like ,

1. Create a file

2. Open a file

3. Read its contents,

4. Modify file contents

5. Close the file.

In Python file handling operations are quite easy.

Lets explore some file operations below. 

How to create a file ?


A file can be opened by using a Open() built-in function.

Open() takes multiple arguments

To start with lets consider most common arguments.

open(file, mode)


file – path to file, this is mandatory parameter

mode – read/write/append, binary or text,

mode parameter is optional, but based on file operation became essential.

Possible values of mode are


r – read only mode (default)

w – write mode

x – create a new file and open if for writing

a – append mode

r+ – read and write

w+ – write read

a+ – append read

b – binary mode

t – text mode

+ – read and write, same as r+

Lets do fallowing file operations.

1. Create a file

2. Write a something to it

3. Read the file



1.Creating a file.


Here we are going to create a text file named ‘first_file.txt’, notice the mode is ‘x’

f = open('first_file.txt','x')


That’s it, you have created ‘first_file.txt’ at the current directory.

The open() function creates a file object and returns file handle. The ‘f’ variable holds the reference of the file object.

We can check the type of the file object.



lt;class '_io.TextIOWrapper'gt;


Its an object of TextIOWrapper class.

As we have open the file, lets write few things in to the file.


2.Writing to a file.


Writing to file is also, very simple.

f.write("This is first line")




write() is a file method, it writes the string to the file

and returns the number of characters written.

Lets see whether its actually written to the file.

For that we will close the file first, and open it again in read mode.






close() method, flushes and closes the IO object.

If the file is already closed does nothing.

You need to close the file, once the read() is complete.

closing file is important, as file object is an OS resource,

there is limitation of number of fds to be opened on OS.

Once the file is closed, if you try to write in to file

it gives and ValueError


f.write("This is second line")
ValueError                                Traceback (most recent call last)
lt;ipython-input-5-9892e63dac30gt; in lt;modulegt;()
----gt; 1 f.write("This is first line")
      2 f.close()
      4 f.write("This is second line")

ValueError: I/O operation on closed file.


To write in to the file again we have to again open it,

in write or append mode, which we will seen in bit.


Now lets try to read the contents what we have written.

3.Reading a file.


Open the file in read mode.

f = open('first_file.txt', 'r')


Now lets the read the contents of the file by using read() method.


'This is first line'



Reads at most ‘n’ characters from the stream.

If ‘n’ is given, the read () method,  reads the whole file contents as one big string.

If you call read() again, returns an empty string.

Now try and write some more contents in to the file.

like before we have to close the file and reopen it in write mode.

f = open('first_file.txt','w')

f.write('This is second line')

#read 10 characters form the file

f = open('first_file.txt')


This is fi
f = open('first_file.txt') 

# with parameter reads whole file



'This is second line' 


Notice the contents of the file changed.

Now we have a new text, but previous text is gone.

Because, when you open a file in write only mode,

it overwrites the previous content.

To avoid this we have open the file in append mode, ‘a’

lets do that.

Appending to a file.


Open the file in append mode.


open the file in append mode and write.

f = open('first_file.txt','a')

f.write('This is third line')



'this is second linethis is third line'


Notice,’this is third line’ is written immediately after the first line in the file.

If you want a add contents on the line,

you have to explicitly add the newline string to your content.


Write text to file with newline

f = open('first_file.txt','w')

f.write('This is second line\n')

f = open('first_file.txt','a')

f.write('This is third line\n')



And when you print the output of the f.read()

f = open('first_file.txt')

'This is second line\nThis is third line\n'



If you print the f.read() you’ll see the contents in new line.



This is second line
This is third line



As you have noticed  you need to have right mode for reading and writing to files,

avoid this we can use ‘r+’ and ‘w+’ modes.

We can also read file contents line by line by using readline() and readlines() methods.


readline() – Retunrs one line




readlines() – Returns a list of all lines.


Note in Python we have only readline(),

there is no writeline() method for file object,

But we have a writelines() method similar to readlines().

Before that we will write few more lines to our file and then read contents line by line.

f = open('first_file.txt','w')

f.write('This is first line\n')

f.write('This is second line\n')

f.write('This is third line\n')

f.write('This is fourth line\n')

f.write('This is fifth line\n')

f.write('This is sixth line\n')

f.write('This is seventh line\n')

f.write('This is eighth line\n')

f.write('This is ninth line\n')

f.write('This is tenth line\n')




Reading lines from file



Open the file for reading.


f = open('first_file.txt')


['This is first line\n', 'This is second line\n', 'This is third line\n', 
'This is fourth line\n', 'This is fifth line\n', 'This is sixth line\n', 
'This is seventh line\n', 'This is eighth line\n', 'This is ninth line\n', 
'This is tenth line\n']



This is first line



This is second line



Like this we can access all the individual lines.

We can also read write from a particular position inside a file.

For this we have two handy methods seek() and tell().



seek() – Changes the stream position to the given offset,

relative the whence.

Whence can have below value

0: start of the stream(file)

1: current stream position

2: end of stream, here offset usually a negative number.



tell()- Returns the current stream position.

Before we explore seek() and tell(),

We need to understand, a file can be opened in text or binary mode.

We can use ‘t’ or ‘b’ along with other file modes ‘r’,’w’,’a’.

‘t’ is text mode, and ‘b’ if for binary mode.


Binary mode


File opened in binary mode are treated as byte objects.

Operations are done on byte objects without any decoding,

and data is treated as raw data.


Text mode


While files opened text mode, are treated as text strings.

Raw data is encoded or decoded based on,

platform dependent encoding or specified encoding option.

In open() built-in function we can specify encoding, its an optional parameter.


encoding – text encoding, optional parameter in open() built-in function.

At file level a file contains only series of bytes, ones and zeros 1100, 0011, 0000,1000 etc

Python is capable of distinguishing between a file in binary mode and text mode.

You can check the default encoding

#sys is built in module 

#comes with standard installation

import sys   





Here is an example of opening a file in text mode for writing,

with ‘utf-8’ encoding specified.

fd = open('test.txt', mode = 'wt', encoding = 'utf-8')


Lets see some more examples of file opening in text and binary mode.


Ex 1:

open a file in binary mode for reading

f = open('first_file.txt','rb') 


Ex 2:

Open a file in binary mode for writing.

f = open('first_file.txt','wb') 


Ex 3:

open a file in text mode for reading

f = open('first_file.txt','rt') 


Ex 4:

Open a file in text mode for writing.

f = open('first_file.txt','wt') 


As we now understood binary and text mode.


seek() and tell()


We explore how to use seek() and tell()

Ex 1:

f = open('first_file.txt','r')



#check the current position


#move to beginning of the file



#move to 10 bytes from start


#check the current position




This is first line

This is second line

This is first line

rst line


whence value 1, 2 relative position from current and EOF

requires a file to be opened in binary mode. otherwise it will throw an error.

Ex 2:

#move 20 bytes from current position


#check the current position




UnsupportedOperation                      Traceback (most recent call last)
lt;ipython-input-84-2985b44c1bfbgt; in lt;modulegt;()
      1 #move 20 bytes from current position
----gt; 2 f.seek(9,1)
      5 #check the current position

UnsupportedOperation: can't do nonzero cur-relative seeks


so we need to open the file in binary mode.


f = open('first_file.txt','rb')

#move 20 bytes from current position

#check the current position



b'irst line\n'

Ex 3:

#move 10 bytes before EOF


#check the current position




b'enth line\n'


How to iterate through file ?

File is an iterable object, so we can iterate through it.

For this we will use for loop and iterate line by line.


f = open('first_file.txt')

for line in f:



This is first line

This is second line

This is third line

This is fourth line

This is fifth line

This is sixth line

This is seventh line

This is eighth line

This is ninth line

This is tenth line


Other file methods


So far we have covered open(), f.read(), f.write(),

f.close(), readline(), readlines()

where ‘f’ is reference to file file object.

Now see some methods related to file.

readable(), writable(), seekable(),




readable()- To check the file is opened in read mode or not.

Returns True if the file is readable otherwise False.







Returns True if the file is writeable, otherwise False.








seekable() – Returns whether the object support random access.

Returns True if the steam is seekable, else False

Note all streams are not seekable.





with clause, context managers.


We have seen in our earlier examples, we need to close file,

once the read, or write operation is done.

Python provides an easy mechanism to operate with files.

i.e ‘with’ context manager.

by using ‘with’ we can call, anything, which returns a context manager,

like open() built-in function.

‘with’ takes care of closing the file.


with open() as fd:
    print('done with fd')



A file ‘file_two.txt’ is opened ‘with’ clause.

We are writing two lines of text in to the file,

with open("file_two.txt",'tw+') as fd:
    fd.write("with context manager\n")
    fd.write("no need of explict file closgin\n")


notice we have not explicitly closed the file.

Now if you try to read the file.

You will get an error.



ValueError                                Traceback (most recent call last)
lt;ipython-input-99-413027223842gt; in lt;modulegt;()
      3     fd.write("no need of explict file closgin\n")
----gt; 5 fd.read()

ValueError: I/O operation on closed file.



Using with clause to read a file.


with open("file_two.txt",'r+') as fd:

['with context manager\n', 'no need of explict file closgin\n']


That’s pretty much covers the basic file handling operation in Python.