How can I iterate over files in a given directory?

前端 未结 9 883
北海茫月
北海茫月 2020-11-22 04:13

I need to iterate through all .asm files inside a given directory and do some actions on them.

How can this be done in a efficient way?

9条回答
  •  独厮守ぢ
    2020-11-22 04:47

    I'm not quite happy with this implementation yet, I wanted to have a custom constructor that does DirectoryIndex._make(next(os.walk(input_path))) such that you can just pass the path you want a file listing for. Edits welcome!

    import collections
    import os
    
    DirectoryIndex = collections.namedtuple('DirectoryIndex', ['root', 'dirs', 'files'])
    
    for file_name in DirectoryIndex(*next(os.walk('.'))).files:
        file_path = os.path.join(path, file_name)
    

提交回复
热议问题