How to delete a file or directory in Python
How to delete a file or directory in Python.
Here's a step-by-step tutorial on how to delete a file or directory in Python:
Deleting a File
To delete a file in Python, you can use the os.remove()
function from the os
module. Follow these steps:
- Import the
os
module at the beginning of your Python script:
import os
- Use the
os.remove()
function, passing the file path as an argument to delete the file. For example, to delete a file named "example.txt" located in the current directory:
file_path = "example.txt"
os.remove(file_path)
Note: If the file is not found, a FileNotFoundError
will be raised. To avoid this, you can check if the file exists using the os.path.exists()
function before attempting to delete it.
Here's an example code snippet that demonstrates deleting a file:
import os
file_path = "example.txt"
if os.path.exists(file_path):
os.remove(file_path)
print("File deleted successfully.")
else:
print("File not found.")
Deleting an Empty Directory
To delete an empty directory, you can use the os.rmdir()
function. Here's how to do it:
- Import the
os
module:
import os
- Use the
os.rmdir()
function, passing the directory path as an argument. For example, to delete a directory named "example_dir" located in the current directory:
dir_path = "example_dir"
os.rmdir(dir_path)
Note: If the directory is not found or is not empty, a
FileNotFoundError
orOSError
will be raised, respectively. To avoid these exceptions, you can check if the directory exists and is empty using theos.path.exists()
andos.listdir()
functions before attempting to delete it.
Here's an example code snippet that demonstrates deleting an empty directory:
import os
dir_path = "example_dir"
if os.path.exists(dir_path):
if len(os.listdir(dir_path)) == 0:
os.rmdir(dir_path)
print("Directory deleted successfully.")
else:
print("Directory is not empty.")
else:
print("Directory not found.")
Deleting a Directory (Including its Contents)
To delete a directory and its contents (including all subdirectories and files), you can use the shutil.rmtree()
function from the shutil
module. Follow these steps:
- Import the
shutil
module:
import shutil
- Use the
shutil.rmtree()
function, passing the directory path as an argument. For example, to delete a directory named "example_dir" located in the current directory:
dir_path = "example_dir"
shutil.rmtree(dir_path)
Note: Be extra cautious when using
shutil.rmtree()
as it permanently deletes the directory and its contents without confirmation or the ability to recover them.
Here's an example code snippet that demonstrates deleting a directory and its contents:
import shutil
dir_path = "example_dir"
if os.path.exists(dir_path):
shutil.rmtree(dir_path)
print("Directory and its contents deleted successfully.")
else:
print("Directory not found.")
That's it! You now know how to delete files and directories using Python. Remember to exercise caution when deleting files or directories to avoid accidental data loss.