In this tutorial we’ll see how to delete (remove) a file or directory in Python, even recursively deleting a directory. In Python there are different functions and methods in built-in modules that can be used for the purpose.
Deleting a single file– os.remove(), os.unlink(), pathlib.Path.unlink()
Deleting an empty directory– os.rmdir(), pathlib.Path.rmdir()
Deleting non-empty directory– shutil.rmtree()
Deleting a single file in Python using os.remove()
The os module in Python has function remove() that can be used to delete a file. Pass the path of the file to removed as an argument to the function. If path is a directory, OSError is raised so it is better to check if it is a file or not before removing.
import os def delete_file(file_path, dir_path): full_path = os.path.join(dir_path, file_path) if os.path.isfile(full_path): os.remove(full_path) else: print('File %s not found' % full_path)
This function is semantically identical to remove().
import os def delete_file(file_path, dir_path): full_path = os.path.join(dir_path, file_path) if os.path.isfile(full_path): os.unlink(full_path) else: print('File %s not found' % full_path)
The pathlib module is available Python 3.4 onward. This module provides an object oriented way to access file system paths. To delete a file create a Path object that represents a file path and call unlink() method on that object.
from pathlib import Path def delete_file(file_path): path = Path(file_path) if path.is_file(): path.unlink() else: print('File %s not found' % path) #call function delete_file("F:/knpcode/Python/test.txt")
Deleting an empty directory using os.rmdir()
In Python os module there is a function rmdir() that can be used to delete a directory. Only works when the directory is empty, otherwise, OSError is raised.
def delete_dir(dir_path): if os.path.isdir(dir_path): os.rmdir(dir_path) else: print('Directory %s not found' % dir_path)
Deleting an empty directory using pathlib.Path.rmdir()
To delete a directory create a Path object that represents the directory path and call rmdir() method on that object. The directory must be empty in order to be removed.
def delete_dir(dir_path): path = Path(dir_path) if path.is_dir(): path.rmdir() else: print('Directory %s not found' % path)
Deleting non-empty directory using shutil.rmtree()
In order to remove whole directory trees you can use shutil.rmtree(). This function deletes all the subdirectories and the files recursively walking through the directory tree.
Syntax of shutil.rmtree() function is as given below-
There are two arguments with default values. If ignore_errors is true, errors resulting from failed removals will be ignored. If false or omitted handler specified by onerror is called to handle such errors, if that is omitted, they raise an exception.
def delete_dir(dir_path): if os.path.isdir(dir_path): shutil.rmtree(dir_path) else: print('Directory %s not found' % dir_path)
- Python Program to Delete Files Having a Specific Extension
- Python Program to Check File or Directory Exists
- How to Copy File in Python
- Python I/O – Open, Read, Write Files
- Python Program to Read a File
- Python Program to Write a File
- Abstract Class in Python With Examples
- Delete a File or Directory Using a Java Program
That’s all for the topic How to Delete File and Directory in Python. If something is missing or you have something to share about the topic please write a comment.
You may also like