Introduction to File Operations in Python

Rahul Shah Last Updated : 25 Oct, 2024
13 min read

Suppose you are at your desk and prepared to start the next idea that you will be working on with so much energy. Whether it’s analyzing data, automating tasks, or simply organizing your thoughts, one tool stands out: file operations in Python. Imagination file operations as your pocket toolbox that provides you privileges to read, write data with less efforts. We’ll look at the basics of opening files in the different modes, and how to handle files gently with the help of context managers. By the end of this session you should not only know what operations are there in files but also learn to take your programming skills to the next level. Well, let’s start with this great exploration of the concepts and frameworks of file handling in Python!

Introduction to File Operations in Python

Learning Objectives

  • Understand file operations in Python with practical examples.
  • Learn different file opening modes in Python and how to choose the right one.
  • Explore the file concept in Python and how it interacts with system resources.
  • Get insights into Python operators with a downloadable PDF for reference.
  • Perform programming using file operations in Python for reading and writing tasks.
  • Learn how to insert data in MongoDB using Python.
  • Access a Python operators PDF and a file operations in Python PDF for offline learning.

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.

To perform file operations, you need to open a file first. Then, you perform the necessary operations on the file. After you have completed all the desired operations, you need to close the file. Closing the file is necessary to save the changes made during the current session.

List of File Operations in Python

Below we will explore the table of list of file operations in Python:

MethodDescription
open(filename, mode)To work with a file, specify its name and the mode (like reading ‘r’ or writing ‘w’). This returns a file object for your code.
close()When you’re finished with a file, use this method to close it up. It’s super important to do this to free up resources. If you try to do anything with it after closing, you’ll get an error.
read(size=-1)Want to read what’s inside a file? This method pulls the content for you. You can tell it how much to read with the size parameter, but if you don’t, it’ll grab everything in the file.
readline(size=-1)This one reads just a single line from the file. If you want, you can set a size to limit how many characters it reads. If not, it’ll return the whole line for you.
readlines(hint=-1)If you want to get all the lines in a file at once, this method does the trick. It gives you a list of all the lines. You can even set a hint if you want to limit how much it reads, but it can also read until the end.
write(string)Ready to write something to a file? Just use this method and pass in your text. Be careful, though—if you’re in write mode, it might wipe out what was already there! It returns how many characters you wrote.
seek(offset[, whence])This method lets you move around in the file. The offset tells it how far to jump, and you can use whence to tell it where to start counting from: the beginning, your current spot, or the end of the file.
tell()Wondering where you are in the file? This method tells you the current position of the file pointer, which is where the next read or write will happen.
truncate(size=None)Need to cut down the file? This method helps you do that. You can specify a size to truncate it to that length. If you don’t, it will truncate at your current position in the file.

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, the open() function offers other optional arguments that users can utilize as needed. Let’s look at an example of opening a file. Suppose we have a file named myfile.txt on our desktop; we can open it 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 in python file handling available by default. These modes specify in which mode our file needed to be opened. Each file opening modes in python file handling 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

Below are the file opening mode in python. Let us explore each one of them in detail:

ModeDescriptionExample
rThis is the read mode. It’s the default when you open a file. You can only read from the file, and if it doesn’t exist, you’ll get an error.file = open('example.txt', 'r')
wHere’s the write mode! It allows you to write to a file. If the file already exists, it will erase everything in it. If it doesn’t exist, a new file will be created.file = open('example.txt', 'w')
aThis mode allows you to add new content to the end of a file without deleting existing data. If the file doesn’t exist, it will be created.file = open('example.txt', 'a')
xExclusive creation mode! This opens the file for writing, but only if it doesn’t already exist. If the file is there, you’ll see an error.file = open('example.txt', 'x')
rbBinary mode. This opens the file in binary format instead of text. It’s useful for reading non-text files like images or executables.file = open('example.txt', 'rb')
rtText mode. This is the default mode for opening files as text, commonly used for regular text files.file = open('example.txt', 'rt')
r+Update mode. This mode lets you read and write to the same file without truncating it, unless you choose to do so manually.file = open('example.txt', 'r+')

Examples

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 file handling. 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())

Output:

Hello,
how you

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

Output:

Hello,
how you

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 opens again, and the cursor moves to the start point, i.e., index 0. Thus, we read 8 characters 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))

Output:

Hello,
how you

As a result, when you read all the characters of the file and try to use the .read() method again, it will return an empty string.

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

Output:

Hello,
how you

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

Output:

Hello,
how you

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

One of the other useful operation is output or more precisely writing after opening a file after sometime is overwritten. In Python, using writing into a file is simple though requires vigilance. In general, we write into a file by opening it by write mode ‘w’ or append mode ‘a’. We can write into a file using the method called.write() To write into a file we needs to open the file in write mode. When using the write mode ‘W’ there is always the danger of the file being cleared out.

In order to make it easier to comprehend, I will provide and example to go along with it. If we want to add content to our existing file, myfile.txt, then we first open the file in the ‘w’ write mode and then use the .write() method on this file. Before writing into the file, the content must be in the form of a string. Use newline character ‘n’ where there should be a line between two different sentences. This is helpful to the user in terms of readability and is also beneficial if you use the.readline() and.readlines() functions to divide the text.

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

Output:

Hello,
how you

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

While writing into a file, if we open the file in ‘w’ or ‘w+’ mode and it does not exist, the system will create a new file with 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

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

Output:

Hello,
how you

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

Output:

Hello All
I am using append mode

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:

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

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

Output:

Hello,
how you

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

Output:

Hello,
how you

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 method will truncate the file up to the current cursor position.

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

Output:

Hello,
how you

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

Output:

Hello,
how you

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

Understanding the Encoding Parameter

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

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.

Practical Example of General File Management

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

Output:

Hello,
how you

Conclusion

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.

Key Takeaways

  • Python simplifies file handling with built-in functions like open(), supporting reading, writing, and appending.
  • We have flexible file modes like ‘r’, ‘w’, ‘a’, and ‘rb’ for reading, writing, and handling binary files in Python.
  • The with open statement ensures efficient and safe file handling by automatically managing resource cleanup.
  • Python’s MongoDB upsert feature allows seamless updating or inserting of documents using upsert=True.
  • Python libraries like PyPDF2 and reportlab make PDF file manipulation and creation easy.

Frequently Asked Questions

Q1. What are the file operations in Python?

A. In Python, you can perform various file operations, such as opening, reading, writing, and closing files. Additionally, Python allows you to append data to existing files, ensuring flexibility in file handling.

Q2. What are file operations?

A. File operations involve tasks like creating, opening, reading, writing, appending, and closing files. These operations enable you to manage and manipulate files effectively within a program.

Q3. What are the types of files in Python?

A. In Python, you typically encounter two types of files: text files and binary files. Text files store data in human-readable format, whereas binary files store data in a format that requires specific programs to read.

Q4. What is a file in Python with an example?

A. A file in Python represents a storage location on disk. For example, you can create and write to a text file using:
with open(“example.txt”, “w”) as file:
file.write(“Hello, World!”)
This code snippet opens (or creates) a file named “example.txt” and writes “Hello, World!” to it.

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

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

Responses From Readers

Clear

Michael B
Michael B

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

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details