1import os
2import shutil
3
4if os.path.exists("demofile.txt"):
5 os.remove("demofile.txt") # one file at a time
6
7os.rmdir("test_directory") # removes empty directory
8shutil.rmtree("test_directory") # removes not empty directory and its content
9
1import os
2filePath = '/home/somedir/Documents/python/logs'
3
4if os.path.exists(filePath):
5 os.remove(filePath)
6else:
7 print("Can not delete the file as it doesn't exists")
1import os
2os.remove(file) for file in os.listdir('path/to/directory') if file.endswith('.png')
1import os
2if os.path.exists("demofile.txt"):
3 os.remove("demofile.txt")
4else:
5 print("The file does not exist")