{"id":11847,"date":"2022-02-06T16:46:31","date_gmt":"2022-02-06T21:46:31","guid":{"rendered":"http:\/\/local.brightwhiz\/?post_type=snippets&p=11847"},"modified":"2022-02-06T16:46:32","modified_gmt":"2022-02-06T21:46:32","slug":"how-to-delete-file-folder-python","status":"publish","type":"snippets","link":"http:\/\/local.brightwhiz\/snippets\/how-to-delete-file-folder-python\/","title":{"rendered":"How to Delete a File or Folder in Python"},"content":{"rendered":"\n
Using Using Using Using os remove<\/code> to remove a file:<\/p>\n\n\n\n
import os\nos.remove('\/path\/to\/filename.txt')<\/code><\/pre>\n\n\n\n
os system<\/code> to remove a file:<\/p>\n\n\n\n
import os\nos.system(f"rm -rf {filename.txt}")<\/code><\/pre>\n\n\n\n
os rmdir<\/code> to remove an empty directory:<\/p>\n\n\n\n
import os\nos.rmdir('\/path\/to\/folder\/')<\/code><\/pre>\n\n\n\n
shutil rmtree<\/code> to delete a directory and all its contents:<\/p>\n\n\n\n
import shutil\nshutil.rmtree('\/your\/folder\/path\/')<\/code><\/pre>\n","protected":false},"comment_status":"open","ping_status":"closed","template":"","meta":[],"categories":[953,15],"tags":[954,471,475],"yoast_head":"\n