import os
import shutil
if os.path.exists("demofile.txt"):
os.remove("demofile.txt") # one file at a time
os.rmdir("test_directory") # removes empty directory
shutil.rmtree("test_directory") # removes not empty directory and its content
import os
os.remove("filename.txt")
import os
if os.path.isfile('/Users/test/new_file.txt'):
os.remove('/Users/test/new_file.txt')
print("success")
else:
print("File doesn't exists!")
import os
os.remove("/tmp/<file_name>.txt")
import os
ch = os.chdir("Directory") #for example: E:/../../
files = os.listdir()
for file in files:
if "file name .format" in file:
os.remove(file)
#code by fawlid
#!/usr/bin/python
import os
myfile="/tmp/foo.txt"
## If file exists, delete it ##
if os.path.isfile(myfile):
os.remove(myfile)
else: ## Show an error ##
print("Error: %s file not found" % myfile)