Remove all files in a directory

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

    Although this is an old question, I think none has already answered using this approach:

    # python 2.7
    import os
    
    d='/home/me/test'
    filesToRemove = [os.path.join(d,f) for f in os.listdir(d)]
    for f in filesToRemove:
        os.remove(f) 
    

提交回复
热议问题