3/27/2020

How to delete a file or folder? (python code)

Use os or shut library
os.remove() removes a file.
os.rmdir() removes an empty directory.
shutil.rmtree() deletes a directory and all its contents.


EX)
import os
os.remove("/tmp/<file_name>.txt")
os.rmdir("/tmp/")

import shutil
shutil.rmtree("/tmp/")


ex2)
if os.path.isdir('./runs/ex'):
    shutil.rmtree('./runs/ex')
os.mkdir('./runs/ex')

No comments:

Post a Comment