Python Files

In this tutorial, you will master all about python files and file operations with examples. You will cover what a file is, how to open and close a file, and some basic ways to read and write files in python.

What is a file in python?

A file is a named location on the system storage which stores related data for future purposes. The data can be anything like a simple text file, video or audio file, or any complex executable programs. We use the file system to enable persistent storage in non-volatile memory like a hard disk. Files consist of three important parts as listed below.

  • The header holds the metadata of the file i.e., the details like name, size, type, etc of file.
  • Data is the contents in the file like texts, pictures, audio, etc.
  • EOF indicates the end of the file.

Next, when it comes to accessing a file we must know where we have stored the file. For that, we use the File path which gives the route or location of the file. The file path contains 3 parts as listed below.

  • The folder path indicates the location of the folder in which the file resides.
  • Filename indicates the actual name of the file.
  • File extension shows the type of file.

Types of Files in python

There are two types of files in python. They are:

Types of files in python

Types of files in python

  1. Binary Files are files that contain non-text values like images, audios etc. Usually, binary files contain the objects in the form of zeros and ones.
  2. Text Files are files that contain text values that are structured into lines. A text file in short contains multiple lines of text values. Each line ends with a special character referred to as the End of Line.

When you want to work with files, you need to perform the 3 basic operations which are listed below in the order of processing.

  1. Open a file
  2. Read or  write operation
  3. Close a file
Flow chart for file handling

Flow chart for file handling

The above flow chart gives you the workflow of file handling in python. Initially, we need to create a file. This can be done manually by the user wherever he needs and save it with a valid filename and extension. Also, python supports file creation which you will encounter in future sessions.

How to open a file in python

The first and foremost operation with a file is opening the file. A file can be opened by invoking the python built-in function open(). The open function returns the file object which can be used later to read or write the file.

The basic syntax of open() function is :
File object = open(file_name [,access_mode] [,buffersize])

Where,

  • File_name is the mandatory string argument which gives the name of the file.
  • is the optional argument. It determines the files opening mode (r,w, a - read, write, append). Besides these, we can also open files in either text mode or binary mode. Binary mode is represented using the letter ’b’.By default, the mode is set to read as text which returns stings. Contrarily, the binary mode returns bytes as it is used to read non-text files like images or executable files.
  • is another optional argument. By default buffer value is set to 0. When the buffer value is one, line buffering will occur during file accessing. If it is greater than 1, buffering will occur in the size of the buffer.

Example

open("testfile.txt")
open("testfile.txt",'r')
 

Here is the table showing different opening modes of files.

Modes Meaning Description
r Read Default mode.Open file for reading. Error if the file does not exist
w Write Opens file in write-only mode to overwrite. Creates a file if it does not exist
a Append Appends the file at the end of the file without trimming the existing. Creates a file if it does not exist.
x Create Creates the defined file. Error if the file already exists.
b Binary Opens file in binary mode
t Text Opens file in text mode. Default mode.
r+ Read and Write Opens file for both reading and writing
rb Binary read Opens file in read and binary mode
rb+ Binary read and write Opens file in binary mode for reading and writing
w+ Read and write Opens file in writing and reading mode
wb Binary write Opens file in writing and binary mode
wb+ Binary read and write Opens file in binary mode for writing and reading
a+ Append read and write Open file in appending mode for reading and writing.
ab Binary append Opens in append mode and binary mode
ab+ Binary append read and write Opens file in read write binary mode for appending

You can use several attributes to get the details of the file once after a file object gets created. Some of the attributes are listed below.

  • .closed:returns True when the file is closed otherwise returns false
  • .mode: returns the mode of the file with which file is opened.
  • .name: returns the name of the file.
  • .softspace: returns true if explicit space is not required with print statement else false

Note: it is always a good practice to specify the encoding while dealing with a file in text mode.

How to close a file in python

Closing a file is not at all a big task however it is the responsibility of the user to properly close the file once the work gets completed. Python supports two ways to close a file which are described below with examples.

  1. Using close() method

    We can close an opened file using the close() method which is available in python. This free up the resources that were secured with the file.

    f_obj = open(“testfile.txt”)
    ,.......................
    ,.......................
    f_obj.close()
    

    Even though this is an easy way,it has one drawback i.e., using close method alone does not address any errors or exceptions. When an exception is encountered it immediately exits the code without properly closing the file. So what should we do next?

    try...finally, block

    To resolve the above problem we can use try..finally, block which addresses the exceptions whenever it raises and thereby assures the proper closure of the file.

    try:
      f_obj = open(“testfile.txt”)
      ,.......................
      ,.......................
    finally:
    f_obj.close()
    
  2. Using with statement

    In python use of “with the statement “ is considered as the best way to close a file. The with statement involuntarily takes the control to close the file when it exits the “with block”. So we don't need to explicitly specify the close () method in the code. In other words, using “with the statement” boosts the easiness to handle exceptions and allows for flawless codes.

    with open(“testfile.txt”) as f_obj:
      ……………………………………
      …………………………………...
    finally:
    f_obj.close()
    

How to read a file

Once we open a file we can perform the desired operation on it. The two operations that can be done on a file are read and write operation. First, we can look at how to read a file. To read a file, the first and foremost thing we have to do is to open a file in read mode (r). Assume we have the following contents in our file named testfile.txt in the same location in python.

testfile.txt

Hello World
Welcome to Programming Tutorials
We Love Python
See You..Bye

Below given are the three methods that can be called on a file object to read the file.


real(size) method

Using this method we can read a file to the specified size bytes.

f_obj=open("testfile.txt",'r')
print(f_obj.read(6))  #reads the first 6 characters(Hello ) in the file
print(f_obj.read(4))  #reads the next 4 characters(Worl) in the file
f_obj.close()

In the above example, size is specified as 6 so it reads the first 6 characters only and returns the same. The next time the size is mentioned as 4, so it reads the next 4 characters in the file after Hello (first 6 characters). This indicates that the read() method returns a newline after each function call.
The read() method reads the entire file when :

  • None is passed as an argument
  • -1 is specified as the size of the argument
  • The argument is not passed

The following example gives clarity for this concept.

f_obj=open("testfile.txt",'r')
print(f_obj.read(-1)) 

Output:

Hello World
Welcome to Programming Tutorials
We Love Python
See You..Bye

readline(size) method

This method is used to read each line in the file and this will return a line by line output. Let see in the below example how the readline method works.

f_obj=open("testfile.txt",'r')
print(f_obj.readline())  #reads first line in the file 
print(f_obj.readline())  #reads second line in the file 
print(f_obj.readline())  #reads third line in the file 
f_obj.close() 

Output:

Hello World

Welcome to Programming Tutorials

We Love Python

Similarly when arguments passed are None or -1, the function returns each individual line. However, when we specify the size, something different will occur. Instead of returning lines, it returns the characters to the specified size as in the read method.

f_obj=open("testfile.txt",'r')
print(f_obj.readline(6))
print(f_obj.readline(4))
print(f_obj.readline(-1))
f_obj.close() 

Output:

Hello
Worl
d

readlines() method

readlines() method returns all the lines in the form of a list. For each line, end of the line is denoted as \n. The above example can be changed to :

f_obj=open("testfile.txt",'r')
print(f_obj.readline(6))
print(f_obj.readline(4))
print(f_obj.readlines())  #returns remaining lines as a list
f_obj.close()

Output:

Hello
Worl
['d\n', 'Welcome to Programming Tutorials\n', 'We Love Python\n', 'See You..Bye']

Note: When EOF is encountered all the reading modes will return empty values.


Looping over lines in a file

Another way to read a file in line by line manner is by iterating through each line. This can be accomplished using either for loop or while loop with readlines method. The following example will illustrate two different ways to print lines in a file.

with open("testfile.txt",'r') as f_obj:
 for line in f_obj:
  print(f_obj.readlines()) 

Output:

(['Hello World\n', 'Welcome to Programming Tutorials\n', 'We Love Python\n', 'See You..Bye']

with open("testfile.txt",'r') as f_obj:
 for line in f_obj.readlines():
  print(line ,end =' ') 

Output:

Hello World
 Welcome to Programming Tutorials
 We Love Python
 See You..Bye

However, the above example can be changed to another form to simplify it. Here instead of looping the methods, we iterate the file object.

with open("testfile.txt",'r') as f_obj:
 for line in f_obj:
  print(line ,end =' ') 

Output:

Hello World
 Welcome to Programming Tutorials
 We Love Python
 See You..Bye

How to write to a file

To write into a file we need to make use of two modes -write mode and append mode. We have already discussed that a file opened in write mode gets overwritten by erasing all the existing lines. Whereas when a file is opened in append mode it appends the new text into the file without truncating the previous text. The builtin method to write to a file is write() method.

Following examples shows the difference between write mode and append mode :

with open("testfile.txt",'w') as f_obj:
 f_obj.write('OVER WRITES FILES')

When we execute the above code, what happens is the textfile.txt gets overwritten as shown in the image below.

overwritten file

Overwritten File

Now we are opening the same file in append mode and adding some text to the file as given in the example below.

with open("testfile.txt",'a') as f_obj:
 f_obj.write('Here iam opened the file in append mode and appends this statment') 

The output shows a significant difference. Here the file is not overwritten instead of appends the text at the end of the existing text.

append file

opening the file in append mode

The other possible way to find the difference in the file after writing or appending is to use again and open the file in read mode and print the file.

How to create a file

To create a new file in python, open the file with any of the below modes.

x”= create: creates a file, returns an error when a file exists with the same file_ name.
a”= append: creates a file when the defined file does not exist.
w”= write: creates a file when the defined file does not exist

open('New_file_x.txt','x')
open('New_file_a.txt','a')
open('New_file_w.txt','w')

Output:

Create a file

From the above example, it is clear that Python has created 3 text files using the aforementioned modes in your system. Now let's check what will happen when we try to create an existing file. Obviously python will raise an error i.e, FileExistsError as shown below.

open('New_file_a.txt','x') 

Output:

  open('New_file_a.txt','x')
FileExistsError: [Errno 17] File exists: 'New_file_a.txt'

To circumvent this error we can either rename the file to a new name or delete the existing file if it is not an important one

How to rename or delete a file

It is possible to rename or delete an existing file. For renaming, we use the built-in method os.rename() , and for removing a file from the system method os.remove is used. Both these methods reside in the OS module. So it is necessary to import the OS module before performing any of these tasks. The OS module which comes under Python standard utility module helps us to interact with the Operating system and thereby provides the way to use OS-dependent functionality.

Renaming a file

The rename method changes the name of an existing file with a new name. The  basic syntax of the rename method is :

os.rename(‘Current_File_name’,’New_File_name’) 

Example

import os
os.rename('New_file_x.txt','New.txt')
 

The result will be reflected in your system with a change in name of the file.

Deleting a file

We can delete a file in two possible ways. One way is to manually search the file in the system and delete the file. The otherways is by standing in the interactive shell and delete files using python methods.

To delete a file we use the built-in method os.remove() which Syntax is

os.remove(‘File_name’) 

Before attempting to delete a file we need to check whether it exists or not to avoid any unwanted errors. We can use the method os.path.exists() to check the existence of a file as shown in the below example:

import os
if os.path.exists('New_file_a.txt'):
 os.remove('New_file_a.txt')
 print('Removed file')
else:
 print('File doesnot exist')
 

Python File Methods

Throughout this tutorial, we have encountered some of the methods used to manipulate files. The below table gives the whole list of file methods used in python.

Method Description
close() Closes an open file.
fileno() Returns the number of file descriptors.
flush() Flushes the write buffer of the file stream.
isatty() Returns True when the file stream is interactive else false
read() Reads the entire file.
readable() Checks whether the file is readable or not
readline() Reads individual lines in a file.
readlines() Reads the entire file and returns as list
seek() Changes the position of the file
seekable() Checks whether the position is changeable or
tell() Returns the current position of the file
truncate() Resizes the file to a specified size.If not specified, resizes to the current position.
writable() Returns True when the file is writable
write() Writes the defined string to the file.
writelines() Writes a list of strings or lines to the file.