Remove all files in a directory

前端 未结 13 2145
小蘑菇
小蘑菇 2020-12-23 19:19

Trying to remove all of the files in a certain directory gives me the follwing error:

OSError: [Errno 2] No such file or directory: \'/home/me/test/*\

相关标签:
13条回答
  • 2020-12-23 20:18

    os.remove() does not work on a directory, and os.rmdir() will only work on an empty directory. And Python won't automatically expand "/home/me/test/*" like some shells do.

    You can use shutil.rmtree() on the directory to do this, however.

    import shutil
    shutil.rmtree('/home/me/test') 
    

    be careful as it removes the files and the sub-directories as well.

    0 讨论(0)
提交回复
热议问题