How to traverse through the files in a directory?

前端 未结 9 1745
离开以前
离开以前 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 12:01

    import os
    rootDir = '.'
    for dirName, subdirList, fileList in os.walk(rootDir):
        print('Found directory: %s' % dirName)
        for fname in fileList:
            print('\t%s' % fname)
        # Remove the first entry in the list of sub-directories
        # if there are any sub-directories present
        if len(subdirList) > 0:
            del subdirList[0]
    

提交回复
热议问题