How to traverse through the files in a directory?

前端 未结 9 1760
离开以前
离开以前 2020-11-30 11:34

I have a directory logfiles. I want to process each file inside this directory using a Python script.

for file in directory:
      # do something
         


        
9条回答
  •  无人及你
    2020-11-30 11:53

    If you need to check for multiple file types, use

    glob.glob("*.jpg") + glob.glob("*.png")
    

    Glob doesn't care about the ordering of the files in the list. If you need files sorted by filename, use

    sorted(glob.glob("*.jpg"))
    

提交回复
热议问题