Remove all files in a directory

前端 未结 13 2278
小蘑菇
小蘑菇 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:04

    shutil.rmtree() for most cases. But it doesn't work for in Windows for readonly files. For windows import win32api and win32con modules from PyWin32.

    def rmtree(dirname):
        retry = True
        while retry:
            retry = False
            try:
                shutil.rmtree(dirname)
            except exceptions.WindowsError, e:
                if e.winerror == 5: # No write permission
                    win32api.SetFileAttributes(dirname, win32con.FILE_ATTRIBUTE_NORMAL)
                    retry = True
    

提交回复
热议问题