How to traverse through the files in a directory?

前端 未结 9 1778
离开以前
离开以前 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:52

    You could try glob:

    import glob
    
    for file in glob.glob('log-*-*.txt'):
      # Etc.
    

    But glob doesn't work recursively (as far as I know), so if your logs are in folders inside of that directory, you'd be better off looking at what Ignacio Vazquez-Abrams posted.

提交回复
热议问题