File Handling and Operations in Python

Reading Time: 4 minutes

Python is extensively used for file handling and operations performed in a file. It supports multiple programming paradigms such as:

File Handling

Python has its own standard library which is very extensive and offers a wide range of functionalities such as I/O handling and standard solutions to many problems that arise during execution. It uses dynamic typing to verify the type safety of a program at runtime and a garbage collector for memory management. 

Python offers the feature of functional programming languages as well. It has an ability to bind everything in a pure mathematical functions order.  

It supports file handling and allows the users to handle the file to perform several operations in the file like read and write. File handling becomes a very important feature when the data is to be stored permanently in a file. A file is a named location on a disk to store related information. In Python, files are treated in two modes: Text format or Binary format.  Python simplifies the process of file handling and makes it easier for the user to perform file handling operations. To perform operations in the file such as read or write in a file, we first need to open the file. 

Operations performed in a file

Open a file

To open a file in python we use the following function:

open()

This function takes two parameters which are, the name of the file and the mode.

Modes can be:

  1. Read (r) : It opens a file in the read-only mode. 
  2. Write(w) : It opens a file for writing.
  3. Append(a) : It opens a file to append some data.
  4. Create(x) : It creates a new file. 
  5. Read and Write(r+) : Read and write to a file without overwriting it.
  6. Write and Read(w+): Writing and reading a file by overriding its contents.

To open file in default mode, use:

file=open('filename')

To print the contents of the file, you can loop through the file by using a for loop as:

for each in file:

print(each)

The output of the script is:

Reading a file

Reading a file is more like displaying the contents of the file for the user to read. It is similar to the ‘cat’ command which displays the contents of the file. 

To read the contents of a file use the following function:

read()

To read from a file, use the following command:

file = open('filename','r')

print(file.read())

This is one way to perform a read operation. 

The output of the script is:

We can also limit the number of characters we want to read from a file by the following command:

read(n)

file = open('filename','r')

print(file.read(7))

The output of the above script is:

It is very important to close the file after the read operation is performed.

Writing to a file 

To write to a file, we must first open the file. We can either append to a file or overwrite a file.

To append, use the tag ‘a’ along with the open function:

open(filename, ‘a’)

file.write("The content you want to append")

file.close()

The output of the script will be:

To overwrite a file, use the tag ‘w’ along with the open function:

open(filename, ‘w’)

file.write("The content you want to overwrite")

file.close()

The output of the above script is:

These are the basic operations that are performed in the file. Even though python has a garbage collector to clean up un-referenced objects, but it is necessary to close the file after operation is performed.

References

https://docs.python.org/3/tutorial/inputoutput.html

Written by 

Vidushi Bansal is a Software Consultant [Devops] at Knoldus Inc. She is passionate about learning and exploring new technologies.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading