How can I iterate over files in a given directory?

前端 未结 9 939
北海茫月
北海茫月 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:57

    This will iterate over all descendant files, not just the immediate children of the directory:

    import os
    
    for subdir, dirs, files in os.walk(rootdir):
        for file in files:
            #print os.path.join(subdir, file)
            filepath = subdir + os.sep + file
    
            if filepath.endswith(".asm"):
                print (filepath)
    

提交回复
热议问题