Delete files with python through OS shell

前端 未结 4 423
时光取名叫无心
时光取名叫无心 2020-12-30 02:19

Im Tyring to Delete all Files in E:. with wildcard.

E:\\test\\*.txt

I would ask rather than test the os.walk. In windows.

4条回答
  •  不知归路
    2020-12-30 02:58

    A slightly verbose writing of another method

    import os
    dir = "E:\\test"
    files = os.listdir(dir)
    for file in files:
        if file.endswith(".txt"):
            os.remove(os.path.join(dir,file))
    

    Or

    import os
    [os.remove(os.path.join("E:\\test",f)) for f in os.listdir("E:\\test") if f.endswith(".txt")]
    

提交回复
热议问题