Python File Handling

Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

We can use File handling to read and write data to and from the file.

Opening a file

Before reading/writing you first need to open the file. Syntax of opening a file is.

open()  function accepts two arguments filename and mode . filename is a string argument which specify filename  along with it’s path  and mode is also a string argument which is used to specify how file will be used i.e for reading or writing. And f  is a file handler object also known as file pointer.

Closing a file

After you have finished reading/writing to the file you need to close the file using close()  method like this,

Different modes of opening a file are

Modes Description
"r"   Open a file for read only
"w"   Open a file for writing. If file already exists its data will be cleared before opening. Otherwise new file will be created
"a"   Opens a file in append mode i.e to write a data to the end of the file
"wb"   Open a file to write in binary mode
"rb"   Open a file to read in binary mode

Let’s now look at some examples.

Writing data to the file

Note: write()  method will not insert new line ( '\n'  ) automatically like print  function, you need to explicitly add '\n'  to write write()  method.

Reading data from the file

To read data back from the file you need one of these three methods.

Methods Description
read([number])  Return specified number of characters from the file. if omitted it will read the entire contents of the file.
readline()  Return the next line of the file.
readlines()  Read all the lines as a list of strings in the file

Reading all the data at once.

Reading all lines as an array.

Reading only one line.

Appending data

To append the data you need to open the file in 'a'  mode.

Looping through the data using for loop

You can iterate through the file using file pointer.

Binary reading and writing

To perform binary i/o you need to use a module called pickle , pickle  module allows you to read and write data using load  and dump method respectively.

Writing data

Reading data

If there is no more data to read from the file pickle.load()  throws EOFError or end of file error.

In the next lesson we will learn about classes and objects in python.

Other Tutorials

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Leave a Reply

6 Comment threads
5 Thread replies
Most reacted comment
Hottest comment thread
8 Comment authors
PythonGuruAmirdjvhadminLuca Recent comment authors
newest oldest most voted
Notify of

Hi Admin,
one small mistake in example:
>>> f.readines() # read entire content of file at once

missed “l” in method


pickle.load() needs one argument i.e. file pointer
eg: pickle.load(f)


>>> f = open(‘myfile.txt’ ‘r’) # needs a ‘,’ between agruments
>>> for line in f:


I guess that every pickle.load() statement in the Reading data code section would require the pointer to the file as argument..?


In the section where you read from the file, specifically “Reading only one line.”, the comment you have in that section of code is incorrect. You are not reading the entire content of the file at once, you are just reading the next line..


Hello Admin,
The comment in front of f.readline() is not correct.