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.
 
 

type(f)

output:

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(string)

 

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.
 
 

f.close() 

 
 

 

close()

 

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()
      3 
      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.
 
 

f.read()


output:
'This is first line'

 
 

read(n)
 

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')
f.close()

#read 10 characters form the file

f = open('first_file.txt')
print(f.read(10))

output:

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

# with parameter reads whole file

f.read() 

output: 

'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')

f.close()

output:

'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.
 

Example:
 

Write text to file with newline
 
 

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

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

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

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

f.close()

 
 

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


f = open('first_file.txt')
f.read()

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

 

 

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


print(f.read())

output:

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()

readline() – Retunrs one line

 

readlines()

 
 

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')

f.close()

 

 

Reading lines from file

 

 

Open the file for reading.

 
 

f = open('first_file.txt')
print(f.readlines())


output:

['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']


print(f.readline())

output:

This is first line

print(f.readline())

output:

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(offset[,whence])

 

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()

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   

sys.getdefaultencoding()

output:

'utf-8'

 
 

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.

example.

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')

print(f.readline())

print(f.readline())


#check the current position

print(f.tell())


#move to beginning of the file

f.seek(0,0)


print(f.readline())

#move to 10 bytes from start

f.seek(10,0)



#check the current position

print(f.tell())


print(f.readline())


output:

This is first line

This is second line

39
This is first line

10
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

f.seek(9,1)


#check the current position

print(f.tell())

print(f.readline())


output:

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)
      3 
      4 
      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
f.seek(9,1)


#check the current position
print(f.tell())

print(f.readline())


output:

9
b'irst line\n'

Ex 3:

#move 10 bytes before EOF

f.seek(-10,2)

#check the current position

print(f.tell())

print(f.readline())


output:

185
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:

    print(line)

output:

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()

 

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

Returns True if the file is readable otherwise False.

 
 
 

print(f.readable())

output:
True

 
 

writable()

 

Returns True if the file is writeable, otherwise False.
 
 

print(f.writable())

output:

False

 

 

seekable()

 

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

Returns True if the steam is seekable, else False

Note all streams are not seekable.

 
 

print(f.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.

Syntax:
 
 

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

 
 

Example:
 

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.
 
 

fd.read()

output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
lt;ipython-input-99-413027223842gt; in lt;modulegt;()
      3     fd.write("no need of explict file closgin\n")
      4 
----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:
    print(fd.readlines())
	
output.

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

 
 

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