While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
In this tutorial, we’ll work on the different file operations in Python. We’ll go over how to use Python to read a file, write to a file, delete files, and much more. So without any delay, let’s get started.
In the previous tutorial, we used console to take input. Now, we will be taking input using a file. That means, we will read from and write into files. To do so, we need to maintain some steps. Those are
We will also learn some useful operations such as copy file and delete file.
When working with large datasets in machine learning problems, working with files is a basic necessity. Since Python is a majorly used language for data science, you need to be proficient with the different file operations that Python offers.
So, let’s explore some of the Python file operations here.
The first step to working with files in Python is to learn how to open a file. You can open files using the
The open() function in Python accepts two arguments. The first one is the file name along with the complete path and the second one is the file open mode.
Below, I’ve listed some of the common reading modes for files:
Additionally, for the Windows operating system, you can append ‘b’ for accessing the file in binary. This is is because Windows differentiates between a binary text file and a regular text file.
Suppose, we place a text file name ‘file.txt’ in the same directory where our code is placed. Now we want to open that file.
However, the open(filename, mode) function returns a file object. With that file object you can proceed your further operation.
#directory: /home/imtiaz/code.py text_file = open('file.txt','r') #Another method using full location text_file2 = open('/home/imtiaz/file.txt','r') print('First Method') print(text_file) print('Second Method') print(text_file2)
The output of the following code will be
================== RESTART: /home/imtiaz/code.py ================== First Method Second Method >>>
Python offers various methods to read and write to files where each functions behaves differently. One important thing to note is the file operations mode. To read a file, you need to open the file in the read or write mode. While to write to a file in Python, you need the file to be open in write mode.
Here are some of the functions in Python that allow you to read and write to files:
Let’s take an example file “abc.txt”, and read individual lines from the file with a for loop:
#open the file text_file = open('/Users/pankaj/abc.txt','r') #get the list of line line_list = text_file.readlines(); #for each line from the list, print the line for line in line_list: print(line) text_file.close() #don't forget to close the file
Now, that we know how to read a file in Python, let’s move ahead and perform a write operation here with the writelines() function.
#open the file text_file = open('/Users/pankaj/file.txt','w') #initialize an empty list word_list=  #iterate 4 times for i in range (1, 5): print("Please enter data: ") line = input() #take input word_list.append(line) #append to the list text_file.writelines(word_list) #write 4 words to the file text_file.close() #don’t forget to close the file
We can use the shutil module to copy files in Python. This utility allows us to perform copy and move operations in Python on different files. Let’s work on this with an example:
import shutil shutil.copy2('/Users/pankaj/abc.txt', '/Users/pankaj/abc_copy2.txt') #another way to copy file shutil.copyfile('/Users/pankaj/abc.txt', '/Users/pankaj/abc_copyfile.txt') print("File Copy Done")
Python’s shutil module offers the remove() method to delete files from the file system. Let’s take a look at how we can perform a delete operation in Python.
import shutil import os #two ways to delete file shutil.os.remove('/Users/pankaj/abc_copy2.txt') os.remove('/Users/pankaj/abc_copy2.txt')
When you open a file in Python, it’s extremely important to close the file after you make the changes. This saves any changes that you’ve previously made, removes the file from the memory, and prevents any further reads or writes within the program.
Syntax to close an open file in Python:
If we continue on from our previous examples where we read files, here’s how you’d close the file:
text_file = open('/Users/pankaj/abc.txt','r') # some file operations here text_file.close()
Additionally, you can avoid closing files manually if you use the with block. As soon as the with block is executed, the files are closed and are no longer available for reading and writing.
It’s common to receive the FileNotFoundError when working with files in Python. It can be easily avoided by providing complete file paths when creating the file object.
File "/Users/pankaj/Desktop/string1.py", line 2, in <module> text_file = open('/Users/pankaj/Desktop/abc.txt','r') FileNotFoundError: [Errno 2] No such file or directory: '/Users/pankaj/Desktop/abc.txt'
To fix the FileNotFoundError, you simply need to verify that the path you’ve mentioned for the file open method is correct.
These are the file operations on Python. There are many more ways you can use files within Python which includes reading CSV data and more. Here’s an article on how you can use the Pandas module to read CSV datasets in Python.
I hope you enjoyed reading the article! Happy learning :)
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up
Click below to sign up and get $100 of credit to try our products over 60 days!