Skip to content

Remove files and directories

For removing files and directories, there are 3 commands:

To remove a single file, use os.remove():

import os
os.rmdir("logs")

To remove an empty directory, use os.rmdir():

import os
os.remove("log_file.log")

To remove a directory and all its contents (in other words, delete recursively), use shutil.rmtree():

import shutil
shutil.rmtree("logs")

(source)

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.