Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
in Python by (3.9k points)
How can I delete a file or a folder in Python?

2 Answers

0 votes
by (46k points)
edited by

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
        raise ValueError("file {} is not a file or dir.".format(path))

0 votes
by (106k points)

You can use the below-mentioned code to delete a file or folder:-

import os


You can use the following video tutorials to clear all your doubts:-

Browse Categories