Introduction to File Operations in Python

Rahul Shah 17 Apr, 2024 • 13 min read

In Python, file operations are essential for reading and writing data to files, and they play a crucial role in data manipulation, analysis, and storage. In this article, we’ll explore the basics of file operations in Python, including how to open, read, write, and manipulate files, along with some advanced techniques to handle files efficiently. We’ll also discuss different file modes, file objects, and file handling best practices that will help you to work with files in Python like a pro.

This article was published as a part of the Data Science Blogathon!

What are File Operations in Python?

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.

A wide range of functions read data in Python is capable to cater the need of file operations of same folder 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 called 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.

List of File Operations in Python

MethodDescription
open(filename, mode)Opens a file and returns a file object. The filename argument is a string that specifies the name of the file to open, and the mode argument specifies the mode in which to open the file (e.g. ‘r’ for read mode, ‘w’ for write mode, etc.).
close()Closes the file object. Any further operations on the file object will raise a ValueError. It’s a good practice to always close files after they have been opened.
read(size=-1)Reads and returns a string from the file. If size is specified, at most size characters will be read. If size is not specified or is negative, the entire file will be read.
readline(size=-1)Reads and returns a single line from the file. If size is specified, at most size characters will be read from the line. If size is not specified or is negative, the entire line will be read.
readlines(hint=-1)Reads and returns a list of lines from the file. If hint is specified, at most hint bytes will be read. If hint is not specified or is negative, all the lines will be read.
write(string)Writes the string to the file. Returns the number of characters written.
writelines(sequence)Writes a sequence of strings to the file. The sequence can be any iterable object (e.g. a list, a tuple, etc.).
seek(offset[, whence])Changes the file position to the given offset. The whence argument is optional and defaults to 0, which means the offset is relative to the beginning of the file. Other values for whence are 1 (relative to the current position) and 2 (relative to the end of the file).
tell()Returns the current file position.
truncate(size=None)Truncates the file to at most size bytes. If size is not specified, the entire file will be truncated.
flush()Flushes the write buffer of the file. This is useful when writing to a file that is being accessed by multiple processes or threads.

Opening a File

Opening a file is the fundamental step in every file types handling a 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:

  • The file path including the file name and the extension we want to open, is to be passed as a string
  • 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:

  1. 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:

  2. 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:

  3. 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.

File Opening Modes in Python

ModeDescriptionExample
rRead mode. Opens the file for reading (default mode). If the file doesn’t exist, an error will be raised.file = open('example.txt', 'r')
wWrite mode. Opens the file for writing. If the file exists, it will be truncated. If the file doesn’t exist, it will be created.file = open('example.txt', 'w')
aAppend mode. Opens the file for writing, but appends new data to the end of the file instead of overwriting existing data. If the file doesn’t exist, it will be created.file = open('example.txt', 'a')
xExclusive creation mode. Opens the file for writing, but only if it doesn’t already exist. If the file exists, an error will be raised.file = open('example.txt', 'x')
bBinary mode. Opens the file in binary mode instead of text mode.file = open('example.txt', 'rb')
tText mode (default). Opens the file in text mode instead of binary mode.file = open('example.txt', 'rt')
+Update mode. Opens the file for both reading and writing.file = open('example.txt', 'r+')

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 Operation in Python

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.

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.

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")

Code

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.

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 File Open 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 File Open. 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 File Open itself.

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

  • The current file name, passed as a string type.
  • 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 file 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 File Open 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.

Frequently Asked Questions

Q1. What is a file handling in Python?

A. File handling in Python is the process of reading from and writing to files using the built-in file objects. It allows developers to work with files to store and manipulate data.

Q2. What are the types of file in file handling in Python?

A. The two main types of files in file handling in Python are text files and binary files. Text files store textual data in ASCII or Unicode format, while binary files store non-textual data such as images or audio files.

Q3. How do you write file handling in Python?

A. To write file handling in Python, you can use the built-in open() function to create a file object, and then use the appropriate file methods to perform read/write operations. Once done, you should close the file using the close() method

Q4. What are the file operations in Python?

A. The file operations in Python File Openinclude opening a file, reading from a file, writing to a file, appending to a file, seeking a specific position in a file, and closing a file. These operations allow developers to manipulate files and their contents in a variety of ways.

Q5. How many types of file operations are there in Python?

A.In Python, file operations are crucial for working with data stored in files. The standard library offers various methods for handling files, such as opening, reading, writing, appending, seeking, flushing, and truncating. These operations allow you to manipulate files effectively, whether you’re storing, retrieving, or modifying data. Whether you’re dealing with a .py file or any other file type, Python’s file handling capabilities make it versatile for managing data across different applications.

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

Rahul Shah 17 Apr 2024

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

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Michael B
Michael B 21 Mar, 2022

Thank you for the article, it was a helpful refresher as I dive deeper into AI/ML learning myself.

Related Courses

Python
Become a full stack data scientist