How to Delete or Remove Files and Directories in Python?

Python offers various inbuilt functions and modules that are used to delete or remove files and directories in Python. Removing files and directories is a very common operation, and it requires great care because once a file/directory is removed, it cannot be recovered.

This write-up will teach you how to delete or remove files and directories in Python. The following aspects are discussed in this Python blog post with appropriate examples:

Let’s start our tutorial!

How to Delete (Remove) Files and Directories in Python

To remove or delete files and directories, the “os.remove()”, “os.unlink()”, “os.rmdir()” and “shutil.rmtree()” functions are used in Python. Some functions are used to delete files only, and a few are used to delete directories only.

How to install Kali Linux in Window...

Below is a snippet displaying the path and name of the file that will be removed using the “os.remove()” and “os.unlink()” functions:

Method 1: Remove Files and Directories in Python Using os.remove()

In the example given below, the files will be removed by using the “os.remove()” function of the os module in Python. Let’s look at the below example to delete the specific file from the given path:

Code:

import os

# Remove the file using os.remove()
os.remove('F:\itslinuxfoss\sample.txt')

In the above code:

  • The “os” module is imported at the beginning of the code.
  • The “os.remove()” function accepts the “complete path” of the file with its “name” along with a complete path to remove the file from the directory.
  • The “os.remove()” only deletes the directory files, not the directory itself.

Output:

From the above output, it can be verified that the file named “sample” has been deleted.

Method 2: Remove Files in Python Using os.unlink()

In the example code given below, the “os.unlink()” function is used to remove the specific file from the given directory.

Code:

import os

# Remove the file using os.unlink()
os.unlink('F:\itslinuxfoss\sample.txt')

In the above code:

  • The “os” module is imported at the start of the program to access the “os.unlink()” function.
  • The “os.unlink()” function is also similar to “os.remove()”, it only deletes files from the directory, not the directory itself.

Output:

In the above output, a file named “sample” has been removed from the selected directory.

Method 3: Remove Empty Directory in Python Using os.rmdir()

In the example given below, the “os.rmdir()” method is used to delete the “empty” directories in Python.

The above snippet shows the path and the name of the empty directory used in the below example code.

Code:

import os

# Remove the directory using os.rmdir()
os.rmdir('F:\itslinuxfoss\sample')

In the above code:

  • The “os.rmdir()” takes the complete path of the directory as an argument and removes the given directory.
  • The “os.rmdir()” only deletes the empty directory; the directory with files will return an error.

Output:

In the above output, the empty directory has been removed from the specified path.

Method 4: Remove Non-Empty Directory in Python Using shutil.rmtree()

In the example given below, the “Directory” containing files will be deleted using the “shutil.rmtree()” function.

Detailed information about the directory is shown in the above snippet.

As you can verify from the above snippet that the directory “sample” contains one file “itslinuxfoss” inside it.

Code:

import shutil

# Remove the directory using shutil.rmtree()
shutil.rmtree('F:\itslinuxfoss\sample')

Let’s describe the above code:

  • To access the “shutil.rmtree()” function, the “shutil” module is imported at the start of the program.
  • The “shutil.rmtree()” takes the file path and the directory name as a parameter.
  • The “shutil.rmtree()” deletes the directory along with the files present within it.

Output:

The above output verified that the directory named “sample” had been completely removed, including its content.

That’s all from this Python Guide!

Conclusion

To remove or delete files and directories the “os.remove()”, “os.unlink()”, “os.rmdir()” and “shutil.rmtree()” are used in Python. The “os.remove()” and “os.unlink()” function only remove files from the given directory. The “os.rmdir()” function removes only empty directories and does not delete a single file separately. The “shutil.rmtree()” function of the “shutil” module removes non-empty directories in Python. This article presented different approaches for removing or deleting files and directories in Python.