crown icon
RAHUL SHAH — October 27, 2021
Beginner Programming Python
This article was published as a part of the Data Science Blogathon

A file is an essential data item stored in one’s computer. Each file can be characterized with its filename & file extension. Python programming language is capable of doing wonders and with time we see a lot of its applications in various domains. One of its wonders handling and organization of files which would save a lot of time for us.

File Operations in Python 1
Image 1

 

A wide range of functions in Python is capable to cater the need of file operations such as opening, reading, writing, creating files et cetera. In the following guide, we will go through the basic yet most necessary operations which are highly useful during every file handling task.

For performing file operations, a file needs to be opened first. Then follows the necessary operations to be performed by the user on our file. After all the desired operations are performed, the file needs to be closed. Closing a file is necessary as it would save the changes made on our file from the current session.

 

Table of Contents

  1. Opening a File
  2. Reading a File
  3. Writing / Creating a File
  4. Closing a File
  5. Cursor Positioning Methods
  6. Truncating a File
  7. Renaming a File
  8. Deleting a File
  9. Extras: The encoding Argument
  10. Extras: File Handling using try-except blocks
  11. A Practical Example
  12. Conclusions

 

Opening a File

Opening a file is the fundamental step in every file handling task. This makes sense because, in any file explorer, we first open before performing any reading or writing operations to it.

Files in Python can be opened with a built-in open() function. Ideally, it takes two string arguments:

1. The file path including the file name and the extension we want to open, is to be passed as a string

2. The mode in which we want to open the file, to be passed as a string.

Thus, the syntax for opening a file would look like this:

open("", "")

Indeed there are other arguments as well in the open() function which are optional and are used as per the requirement of the user. Let’s look at an example for opening a file. Suppose if we have a file named myfile.txt on our desktop, it can be opened by:

open("C:UsersRahulDesktopmyfile.txt")

Unquestionably, if our current working directory of the Python file is the same as our file (here desktop), there is no need to specify the full path. In such a case, our open() function would look like:

open("myfile.txt")

For the ease of code implementation, let us consider our current working directory the same as the location our text files are present for the rest of the article.

Python has a wide range of file opening modes available by default. These modes specify in which mode our file needed to be opened. Each file opening mode comes with its default functionality and one can select the appropriate model based on the need. As discussed above, the file opening mode argument is an important factor that decides whether we want to read, write or update the contents of an existing file.

Let’s look at each of these modes:

1. In ‘r’ mode, the file opens in the read mode. By default, if we don’t specify the mode argument, it will be treated as read or ‘r’ mode. For Example, To open our file myfile.txt in ‘r’ mode:

open("myfile.txt") or open("myfile.txt", "r")

2. In the ‘w’ mode, the file opens in write mode. It removes existing content, if present, in the file. If the specified file doesn’t exist, it creates one with the specified name. For Example, To open our file myfile.txt in ‘w’ mode:

open("myfile.txt", "w")

3. In the ‘a’ mode, the file opens in append mode. It adds content to an existing file (or append at the end of the file). If the specified file doesn’t exist, it creates one with the specified name. It does not remove the existing content from the file. For Example, To open our file myfile.txt in ‘a’ mode:

open("myfile.txt", "a")

4. In the ‘r+’ mode, the file opens in the read & write mode. It does not remove existing content, if present, in the file and does not create a file if not present in the directory. For Example, To open our file myfile.txt in ‘r+‘ mode:

open("myfile.txt", "r+")

5. In the ‘w+’ mode, open the file in the read & write mode & remove existing content. If the file doesn’t exist, it creates a new one. It does not remove existing content & if a file doesn’t exist, it creates a new one. For Example, To open our file myfile.txt in ‘w+’ mode:

open("myfile.txt", "w+")

6. In the ‘a+’ mode, the file opens in the read & append mode. It does not remove existing content & If the file doesn’t exist, it creates a new one. For Example, To open our file myfile.txt in a+ mode:

open("myfile.txt", "a+")

The modes discussed above are being used on a text file. To use these modes for a binary file, we need to use a different combination of file opening mode arguments. Using ‘b’ with any mode, For example, ‘ab’, ‘rb’, ‘wb’, ‘rb+’, the file opens in binary mode. It is used for non-textual files like image, sound, executable (.exe) files.

For Example, To open our file myfile.txt in b mode:

open("myfile.txt", "rb")

Opening a file is not enough. This open function needs to be stored into a variable as this will be used later for writing & reading the file. Here we will assign a variable to our file:

file = open("myfile.txt", "r")

Now that we have learned to open a file, we will look at how to read from a file.

Reading a File

Now that we have opened a file, we can start performing operations to it. In this section, we will look at the reading functionality of files using Python. Before start reading the file, as discussed, we must open the file in ‘r’ mode, since we are going to read the contents of the file. Let us assume that our file myfile.txt already has some textual data present in it.

file = open("myfile.txt", "r")
print(file.read())

On executing this code, we get:

Reading a File | File Operations in Python
Source – Personal Computer

The .read() method to our variable file gives the content of the file as output. We can also specify the number of characters we want to read at once.

file = open("myfile.txt", "r")
print(file.read(8))

On executing this code, we get:

Reading a File 2 | File Operations in Python
Source – Personal Computer

This will print the 8 characters from the point until the file has been read earlier.

Since we executed the open statement before the .read() method, the file is opened again and the cursor is moved at the start point, i.e. index 0. Thus 8 characters are being read from the start point.

Let’s understand this with another example. Given our file named myfile.txt having some content “Hello, how you all have been doing?”. After opening a file, if we use print(file.read(6)), it will first give “Hello,” as output & if use print(file.read(8)), it will start reading 8 characters file from the point where the first read function left off i.e. ” how you”

file = open("myfile.txt", 'r')
print(file.read(6))
print(file.read(8))

On executing this code, we get:

Hello, how you | File Operations in Python
Source – Personal Computer

As a result, when all the characters of the file are being read & if we try to use the .read() method again, it will give an empty string as output.

The .readline() method prints each line from our file, every time we execute this function, until the end of the file is reached. Let’s add some additional lines to our text file to illustrate this example.

file = open("myfile.txt", 'r')
print(file.readline())
print(file.readline())

On executing this, we get:

.readline
Source – Personal Computer

This will print the first two lines of our file, separated by a newline character.

The .readlines() method always gives a list of all the lines of our file as list elements.

file = open("myfile.txt", 'r')
print(file.readlines())

On executing this we get:

Reading a File 4 | File Operations in Python
Source – Personal Computer

A major difference between the .read() method and the .readline() method is, .read() method focuses on reading each character from a file while the .readline() focuses on reading individual lines from a file.

Now let’s look at another important yet vital file operation i.e. writing and creating a file.

 

Writing/Creating a File

Another useful operation widely used, after opening a file, is writing into a file. Writing into a file in Python is easy but an attentive task. Writing into a file is typically done by opening the file in write ‘w’ or append ‘a’ mode. Writing into a file can be done using the .write() method to our file. Caution needed to be followed while using the ‘w’ write mode as it can erase existing content present in the file.

Let’s understand this with an example. If we want to add content to our existing file, myfile.txt, first we need to open it in the ‘w’ write mode and use the .write() method on our file. The content to be written into the file needs to be passed as a string. It’s a good practice to add a newline character ‘n’ to add a line between sentences. This improves the readability for the user and is also useful while using the .readline() and .readlines() methods to distinguish between the lines.

file = open("myfile.txt", "w")
file.write("Hello Alln")

On executing this code, we get:

Writing/Creating a File 1 | Writing/Creating a File
Source – Personal Computer

Here, on execution, the .write() method returns the number of characters written into the file.

While writing into a file, if the file opened in ‘w’ or ‘w+‘ mode does not exist, a new file will be created in the same name in our present working directory.

For Example:

file = open("file.txt", "w")

On executing this code, we get:

Writing/Creating a File 2
Source – Personal Computer

Here, previously the file file.txt does not exist in the current working directory and later it got created. If file.txt would have already existed, it will erase all the content and the file will become empty.

Another file mode used for writing to the file is the ‘a’ append mode. This creates a new file if the specified file does not exist in the current working directory. Unlike ‘w’ mode, it does not remove the existing content from the file. Instead, it appends or adds the new content at the end of the file. Writing in append mode can be done using the .write() method.
Example:

file = open("myfile.txt", "a")
file.write("I am using append moden")

On executing this code, we get:

Writing/Creating a File | 22
Source – Personal Computer

This will append the new content at the end of existing into our specified file.

 

Now, let’s look at another vital file operation, how to close a file.

Closing a File in Python

At last, after opening and performing the reading, writing operations, it is important to close the file. This is done using .close() method. Let’s understand this with an example:

file = open("myfile.txt", 'r')
print(file.read())
file.close()
Closing a File in Python
Source – Personal Computer

It is always a good practice to close a file after performing desired operations to it. Thus makes sure that all our changes have been saved to the file. In the later sections, where we will try to rename and remove the files. To accomplish these tasks, we must close the files to permit renaming and removing the files.

Cursor Positioning Methods

Python has a couple of essential methods for the positioning of the cursor in our file. The methods are as follows:

1. The .seek() method

The .seek() method in Python is used to change the cursor to a specific position.

file = open("myfile.txt", 'r')
file.seek(0)
file.close()

This will move our cursor to index position 0 & file reading will start from the start point again.

2. The .tell() method

The .tell() method in Python prints the current position of our cursor.

file = open("myfile.txt", 'r')
file.tell()

This will give the position up to which file has been read.

   Cursor Positioning Methods
Source – Personal Computer

Since we have used the .seek(0) method previously to move the cursor at index position 0, we got 0 as output for using the .tell() method.
Now, let’s look at some additional yet essential methods which might come in handy in file handling tasks.

 

Truncating a File

Truncating a File is also possible in Python. By using the .truncate() method, we can truncate the file up to the desired length. Let’s understand this with an example:

file = open('myfile.txt', 'w')
file.truncate(20)
file.close()
Truncating a File
Source – Personal Computer

In this example, we truncated a file up to 20 bytes by passing 20 as an argument into the .truncate() method. This reduced the file size of the file to 20 bytes. Reducing the file size also reduces the contents of the file size. If we don’t specify the size parameter inside the .truncate() method, the file will get truncated up to the current cursor position of the file.

A point to note in the .truncate() method is, the file must be opened in write mode to perform truncating task.

 

Renaming a File

Renaming a file in Python can be done using the os Module and needed to be imported to perform such tasks. The os module in Python has a vast collection of methods to perform all the essential file management tasks in Python itself.

To rename our file, we will use the .rename() method from the os module. The .rename() method takes two arguments:

1. The current file name, passed as a string type.

2. The renamed file name, to be passed as a string type.

Let’s understand this with an example.

import os 
os.rename('myfile.txt', 'ourfile.txt')
   Renaming a File
Source – Personal Computer

Here,  ‘myfile.txt’ is our file’s current name and ‘ourfile.txt’ is the name we want to set.

Since we are just renaming the file and not changing the content of the file, we do not need to open it to perform the renaming task.

 

Deleting a File

We can remove the files from a directory using Python’s versatile os module. The os module has a method .remove() which performs the task. The .remove() method takes a single argument as string type which is our file name. Let’s understated this with an example.

import os
os.remove('myfile.txt')
file = open('myfile.txt', 'r')

On executing this, we get:

   Deleting a File
Source – Personal Computer

Since we deleted this file, we cannot open this again and as a result, getting errors.

 

Extras: The encoding Argument

While opening the file, it may happen that we do not get the desired result or we might get an abnormal result while reading the file. This may happen due to encoding issues in the file. The default encoding used for text files is cp1252 in Windows Operating System.
Thus, it is always good to play safe and use an encoding argument while opening the file.

For Example:

open("myfile.txt", "r", encoding = "cp1252")

 

Extras: File Handling using Try-Except Blocks

Often one forgets to close the file. This may produce errors and may become harmful when you are working on a very big file. In such scenarios, try-except-finally blocks come to the rescue.
We can add the file close method into the finally block so that even if the program execution stops due to an exception, the file will get closed anyway.
Example:

try:
   file = open("myfile.txt")
finally:
   file.close()

Thus, if in any case, an exception is thrown in the try block, finally block will be executed and thus file will be closed.

 

A Practical Example

Now we understood all the operations individually, we must now understand how these functions work together in a general file management task. In the following example, we will understand how these methods are used and in which order would help one performing any file operation task on its own. An important thing to note is every time before shifting from writing into a file to read from file and vice versa, one must close the current file opened in a specific mode & open it again in the mode we want. Now, let us move to the example:

file = open("myfile.txt", 'w')
file.write("This is the Practical ExamplenIt performs reading, writing and closing operations of filen")
file.close()
file = open("myfile.txt", 'r')
print(file.read(10))
file.seek(0)
print(file.readlines())
file.seek(0)
print(file.readline())
file.close()

On executing this code, we get:

this is sh
Source – Personal Computer

Conclusions

In this guide, we learnt about Files and it’s operations in Python. We started with the basic features of a file and covering major operations involved in a file. These major operations involve reading, writing, creating and closing the file. We also looked at cursor positioning operations in a file using the .seek() and .tell() methods. We also looked at few more additional operations in file handling such as renaming, truncating and removing a file using Python built-in functions and os module methods. Later we looked at few extra parameters and a different way of implementing file handling tasks using try-except blocks. At last, we looked at a practical example to understand how all these methods work together in any general task.

File Operation in Python is a great asset for accessing and manipulating files directly into your Python program. Unlike other programming languages, file handling in Python is uncomplicated & pretty straightforward and as already said, can help you save a lot of time. With that said, one can try exploring a few more operations and methods associated with file handling to make their tasks more efficient and productive.

About the Author

Connect with me on LinkedIn.

For any suggestions or article requests, you can email me here.

Check out my other Articles Here and on Medium

You can provide your valuable feedback to me on LinkedIn.

Thanks for giving your time!

 

Image Source-

Image 1 – https://www.pexels.com/photo/briefcase-for-documents-placed-on-table-4792286/

 

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.

About the Author

RAHUL SHAH
crown icon
RAHUL SHAH

IT Engineering Graduate currently pursuing Post Graduate Diploma in Data Science.

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

Leave a Reply Your email address will not be published. Required fields are marked *