Remove all files in a directory

前端 未结 13 2257
小蘑菇
小蘑菇 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 19:54

    #python 2.7
    import tempfile
    import shutil
    import exceptions
    import os
    
    def TempCleaner():
        temp_dir_name = tempfile.gettempdir()
        for currentdir in os.listdir(temp_dir_name):
            try:
               shutil.rmtree(os.path.join(temp_dir_name, currentdir))
            except exceptions.WindowsError, e:
                print u'Не удалось удалить:'+ e.filename
    

提交回复
热议问题