How to delete a file or folder in python with example
This tutorial explains file and folder delete in python with code examples.
File delete is an file operation that delete a file from a directory.
directory delete is an operation that delete files and folders in a directory
Python File delete example
There are multiple ways we can delete a file
remove the file using os.remove() function
- Import the os module into the code
- Check file exists or not using
os.path.isfile()
function - if a file exists, Remove the file by calling os.remove() function
- else return file does not exist message
Here is an example
import os
if(os.path.isfile("./emp1.json")): # remove a file using remove
os.remove("./emp1.json")
print("File removed")
else:
print("File does not exist")
delete single file using os.unlink() function `os.unlink() function internally calls os.remove() function. It is similar to the unix unlink operation.
- Import os module into the code
- Check file exists or not using
os.path.isfile()
function - if file exists, Remove the file by calling os.unlink() function
- else return file does not exist message Here is an example
import os
if(os.path.isfile("./emp1.json")): # remove a file using unlink
os.unlink("./emp1.json")
print("File removed")
else:
print("File does not exist")
- Delete a file using pathlib.Path.unlink function
Python 3.4 introduced Path
class in pathlib
module. pathlib.Path.unlink()
removes the file.
- Create a Path Object
- Call calling path.unlink() to remove a file
Here is an example
import os
from pathlib import Path
file = Path("./emp1.json")
if(os.path.isfile("./emp1.json")): # remove a file
file.unlink()
print("File removed")
else:
print("File does not exist")
How to delete Empty folder or directory in Python example
To delete an empty directory in a python, Please follow the below steps
- Import
os
module into codebase. - call
os.rmdir()
orPathlib.rmdir()
function to remove a empty directory - It removes a directory and returns None.
- It throws FileNotFoundError if the directory not exists using os.rmdir() function:
import os
print(os.rmdir("directory1"))
using pathlib.rmdir() function:
import pathlib
directory = pathlib.Path("directory")
directory.rmdir()
How do you delete files and subfolders in directory
To delete a directory and files inside a directory in python, Please follow the below steps
- Import
os
andshutil
moduled into codebase. - Create a path os.path.join(abspath,directory) with abspath is a absolute path location,directory is a directory to remove
- call
shutil.rmtree(path)
function to remove a directory - It removes a directory and it’s files and returns None.
- It throws FileNotFoundError if directory not exists
Here is an example
import os
import shutil
path = os.path.join("./", "directory")
shutil.rmtree(path)
How do you delete a file in Python?
To delete a file in python, Please follow the below steps
- Import os module into codebase.
- call os.remove() to remove a file
- It removes a file and returns None.
- It throws FileNotFoundError if file not exists File can be deleted using os.remove() function