Remove all files in a directory

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

    This will get all files in a directory and remove them.

    import os
    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    dir = os.path.join(BASE_DIR, "foldername")
    
    for root, dirs, files in os.walk(dir):
      for file in files:
        path = os.path.join(dir, file)
        os.remove(path)
    

提交回复
热议问题