Python File Handling Introduction

Files Handling Definition

Most of the programs we have seen so far are transient in the sense that they run for a short time and produce some output, but when they end, their data disappears. If you run the program again, it starts with a clean slate.

Other programs are persistent: they run for a long time (or all the time); they keep at least some of their data in permanent storage (a hard drive, for example); and if they shut down and restart, they pick up where they left off.

One of the simplest ways for programs to maintain their data is by reading and writing text files. An alternative is to store the state of the program in a database.

Text Files

A text file is a sequence of characters stored on a permanent medium like a hard drive, flash memory, or CD-ROM.

Text file contain only text, and has no special formatting such as bold text, italic text, images, etc.

Text files are identified with the .txt file extension.

Reading and Writing to Text Files

Python provides inbuilt functions for creating, writing and reading files.

There are two types of files that can be handled in python, normal text files and binary files (written in binary language,0s and 1s).

Text files: In this type of file, each line of text is terminated with a special character called EOL(End of Line), which is the new line character („\n‟) in python by default.

Binary files: In this type of file, there is no terminator for a line and the data is stored after converting it into machine understandable binary language.

In order to perform some operations on files we have to follow below steps

  • Opening
  • Reading or writing
  • Closing

Here we are going to discusses about opening, closing, reading and writing data in a text file.

File Access Modes

Access modes govern the type of operations possible in the opened file. It refers to how the file will be used once its opened.

These modes also define the location of the File Handle in the file. File handle is like a cursor, which defines from where the data has to be read or written in the file.

There are 6 access modes in python.

    Read Only (‘r’) : Open text file for reading. The handle is positioned at the beginning of the file. If the file does not exists, raises I/O error. This is also the default mode in which file is opened.

    Read and Write (‘r+’) : Open the file for reading and writing. The handle is positioned at the beginning of the file. Raises I/O error if the file does not exists.

    Write Only (‘w’) : Open the file for writing. For existing file, the data is truncated and over-written. The handle is positioned at the beginning of the file. Creates the file if the file does not exists.

    Write and Read (‘w+’) : Open the file for reading and writing. For existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.

    Append Only (‘a’) : Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.

    Append and Read (‘a+’) : Open the file for reading and writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.

Opening a File

It is done using the open() function. No module is required to be imported for this function.


 File_object = open(r"File_Name","Access_Mode")

The file should exist in the same directory as the python program file else, full address (path will be discussed in later section of this unit) of the file should be written on place of filename.

Note: The r is placed before filename to prevent the characters in filename string to be treated as special character.

For example, if there is \temp in the file address, then \t is treated as the tab character and error is raised of invalid address. The r makes the string raw, that is, it tells that the string is without any special characters.

The r can be ignored if the file is in same directory and address is not being placed.?


 f1 = open("sample.txt","a")
 f2 = open(r"D:\python\sample3.txt","w+")

Here, f1 is created as object for sample.txt and f3 as object for sample3.txt (available in D:\python directory)

Closing a File

close() function closes the file and frees the memory space acquired by that file. It is used at the time when the file is no longer needed or if it is to be opened in a different file mode.




 f1 = open("smapl.txt","a")

After closing a file we can't perform any operation on that file. If want to do so, we have to open the file again.

Reading from a File

To read the content of a file, we must open the file in reading mode. There are three ways to read data from a text file.

    read() : Returns the read bytes in form of a string. Reads n bytes, if no n specified, reads the entire file.[n])

    readline() : Reads a line of the file and returns in form of a string. For specified n, reads at most n bytes. However, does not reads more than one line, even if n exceeds the length of the line.


    readlines() : Reads all the lines and return them as each line a string element in a list.



Consider the content of file sample.txt that is present in location D:\python\code\ as

 Read Only
 Read and Write
 Write OnlyWrite and Read
 Append Only
 Append and Read

Now execute the following file reading script.


'Read Only\nRead and Write\nWrite Only\nWrite and Read\nAppend Only\nAppend and Read'

Here \n denotes next line character. If you again run the same script, you will get empty string. Because during the first read statement itself file handler reach the end of the file. If you read again it will return empty string

So in order to take back the file handler to the beginning of the file you have to open the file again or use seek() function. We will discuss about seek function in upcoming section.

 'Read Only\n'
 'Read Only\n'
 ['Read Only\n', 'Read and Write\n', 'Write Only\n', 'Write and Read\n', 'Append Only\n','Append and Read']

File Positions

    tell(): The tell() method tells you the current position within the file; in other words, the next read or write will occur at that many bytes from the beginning of the file.

    seek(): The seek(offset[, from]) method changes the current file position. The offset argument indicates the number of bytes to be moved. The from argument specifies the reference position from where the bytes are to be moved.

If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the current position as the reference position and if it is set to 2 then the end of the file would be taken as the reference position.

If the second argument is omitted, it also means use the beginning of the file as the reference position.


 >>> f1=open("D:\python\code\sample.txt","r")
 >>> f1.tell()
 >>> f1.readline()
 'Read Only\n'
 >>> f1.tell()
 >>> f1.tell()
 >>> f1.tell()
 >>> f1.readline()

Writing to a File

In order to write into a file we need to open it in write 'w' or append 'a'. We need to be careful with the 'w' mode as it will overwrite into the file if it already exists. All previous data are erased.

There are two ways to write in a file.

    write() : Inserts the string str1 in a single line in the text file.


    writelines() : For a list of string elements, each string is inserted in the text file.Used to insert multiple strings at a single time.

     File_object.writelines(L) for L = [str1, str2, str3]	


 >>> f4=open("fruit.txt","w")
 >>> fruit_list=['Apple\n','Orange\n','Pineapple\n']
 >>> f4.writelines(fruit_list)
 >>> f4.write('Strawberry\n')
 >>> f4.close()
 >>> f4=open("fruit.txt","r")

Appending to a File

Adding content at the end of a file is known as append. In order to do appending operation, we have to open the file with append mode.


 >> f4=open('fruit.txt','a')
 >>> f4.write('Banana')
 >>> f4.close()
 >>> f4=open('fruit.txt','r')

The File Object Attributes

Once a file is opened and you have one file object, you can get various information related to that file. Here is a list of all attributes related to file object:

Attribute Description
File_object.closed Returns true if file is closed, false otherwise.
File_object.mode Returns access mode with which file was opened. Returns name of the file.
File_object.softspace Returns false if space explicitly required with print, true otherwise.