Python Deleting Files/Directories in Python:

Deleting Files

The remove() method of os module deletes the file in the given path. However, if the file does not exist, it will raise a FileNotFoundError exception. This function can support path relative to directory descriptors.

Method Signature

The signature for the remove method is as shown below. This method is called with module name. Here, os refers to the os module.

os.remove(path, dir_fd)

Method parameters and return type

  • Here, the remove method takes two parameters. The parameter path represents the file path. The dir_fd is an optional parameter that represents a file descriptor referring to a directory. However, it ignores the dir_fd parameter for the absolute path.
  • It doesn’t return anything.

Python remove method Example:

Example 1: In this case, test.txt file lies in the same folder as the python script. So, we will just mention the file name.

import os 
  
# Remove the file 
os.remove("test.txt") 
print("File removed successfully")

Output

File removed successfully

Deleting an Empty Directory

The rmdir() method of os module deletes an empty directory. However, if the directory does not exist or is not empty, then it will raise a FileNotFoundError or an OSError exception respectively. This function can support path relative to directory descriptors.

Method Signature

The signature for the rmdir() method is as shown below. This method is called with module name. Here, os refers to the os module.

os.rmdir(path, *, dir_fd=None)

Method parameters and return type

  • Here, the rmdir() method takes path as a parameter that represents the directory path, and the dir_fd is an optional parameter that represents the file descriptor referring to a directory. However, it ignores the dir_fd parameter for the absolute path.
  • However, it doesn’t return anything.

Python rmdir method Example:

Example 1: In this case, it deletes the temp folder if and only if it is empty. Otherwise, it will raise an OSError exception.

import os
# Deleting an empty folder
d = r"D:\pythondemo\temp"
os.rmdir(d)
print("Directory deleted successfully")

Output

Directory deleted successfully

Deleting a Non-Empty Directory

The rmtree() method of shutil module deletes a non-empty directory along with all the files in that directory.

Method Signature

The signature for the rmtree() method is as shown below. This method is called with module name. Here, shutil refers to the shutil module.

shutil.rmtree(path, ignore_errors=False, onerror=None )

Method parameters and return type

  • Here, the rmtree method takes path as a parameter that represents the directory path. If the ignore_errors flag is set to false, then it ignores the errors due to failed removals.
  • It doesn’t return anything.

Python rmtree method Example:

Example 1: In this case, It will delete all the files and directories inside temp folder.

import shutil
# Deleting an empty folder
d = r"D:\pythondemo\temp"
os.rmtree(d)
print("Directory deleted successfully")

Output

Directory deleted successfully

Conclusion

Hence, the remove() method removes the given file. Whereas, the rmdir() method will delete an empty directory and the rmtree() method removes the non-empty directory.

References

Happy Learning 🙂

Leave A Comment