How to delete a file in Python

  • Reading time:5 mins read

How to delete a file with the help of the OS module?

In this article, we will explore how to delete a file in Python using the OS module. The Python OS module in Python provides an easier way for users to interact with the underlying operating system.

The OS module provides many functions that enable users to perform various actions. Some of these actions may include creating, moving and deleting files and directories in Python programs.

 

Using OS functions to delete a file

In order to use the OS module we only need to import it at the top of our program. This is because the OS module is already provided in the standard library. Now using the functions os.remove() and os.rmdir() we can remove files and directories respectively in Python programs. 

 

python delete a file

 

In the program below we have a folder named ‘emptyfolder’ containing a text file ‘myfile.txt’ as well as a python file named ‘deleting.py’. Now using the os.remove() we can remove the file ‘myfile.txt’ from a Python program by specifying the name of the file that we intend to remove within the parentheses as shown below.

 

 

Removing a directory using the os module

Besides removing files we can also remove directories in Python programs using the os module. In a similar manner, we need to specify the name of the directory that we want to remove within the parentheses of the os.rmdir() method.

However, before we can proceed to use this method we need to ensure that the folder that we want to remove does not contain any files.

 

Delete a file by using the remove() function

It is worth noting that the os.remove() returns the error FileNotFoundError if the file that we want to remove does not exist. In the code below we are getting a FileNotFoundError since the file named ‘myfile.txt’ that we initially removed is no longer existing in the folder.

 

Delete a file in a try-except block

Using the try and except method, we can handle this error and make sure that it does not stop our program from running. The try and except method also allows us to return a message that the user can understand. 

 

 

The try block in this case tests whether the file that we are trying to remove exists and goes ahead to remove the names file if so. On the other hand, if the file does not exist the except block will be executed in order to avert the error that is being raised and instead return a message to the user that the file in question does not exist.

 

python delete a file

 

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.