Back
I've found a method to do it and it really helped me to delete the files or folders in Python,
def remove(path): """ param <path> could either be relative or absolute. """ if os.path.isfile(path): os.remove(path) # remove the file elif os.path.isdir(path): shutil.rmtree(path) # remove dir and all contains else: raise ValueError("file {} is not a file or dir.".format(path))
You can use the below-mentioned code to delete a file or folder:-
import osos.remove("/tmp/<file_name>.txt")
import os
os.remove("/tmp/<file_name>.txt")
You can use the following video tutorials to clear all your doubts:-
30.9k questions
32.9k answers
500 comments
665 users