How can I iterate over files in a given directory?

2019-01-01 06:48发布

问题:

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?

回答1:

Original answer:

for filename in os.listdir(directory):
    if filename.endswith(\".asm\") or filename.endswith(\".py\"): 
        # print(os.path.join(directory, filename))
        continue
    else:
        continue

Python 3.6 version of the above answer, using os - assuming that you have the directory path as a str object in a variable called directory_in_str:

directory = os.fsencode(directory_in_str)

for file in os.listdir(directory):
    filename = os.fsdecode(file)
    if filename.endswith(\".asm\") or filename.endswith(\".py\"): 
        # print(os.path.join(directory, filename))
        continue
    else:
        continue

Or recursively, using pathlib:

from pathlib import Path

pathlist = Path(directory_in_str).glob(\'**/*.asm\')
for path in pathlist:
    # because path is object not string
    path_in_str = str(path)
    # print(path_in_str)


回答2:

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)


回答3:

You can try using glob module

import glob

for filepath in glob.iglob(\'my_dir/*.asm\'):
    print(filepath)


回答4:

Python 3.4 and later offer pathlib in the standard library. You could do:

from pathlib import Path

asm_pths = [pth for pth in Path.cwd().iterdir()
            if pth.suffix == \'.asm\']

Or if you don\'t like list comprehensions:

asm_paths = []
for pth in Path.cwd().iterdir():
    if pth.suffix == \'.asm\':
        asm_pths.append(pth)

Path objects can easily be converted to strings.



回答5:

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)


回答6:

Here\'s how I iterate through files in Python:

import os

path = \'the/name/of/your/path\'

folder = os.fsencode(path)

filenames = []

for file in os.listdir(folder):
    filename = os.fsdecode(file)
    if filename.endswith( (\'.jpeg\', \'.png\', \'.gif\') ): # whatever file types you\'re using...
        filenames.append(filename)

filenames.sort() # now you have the filenames and can do something with them

NONE OF THESE TECHNIQUES GUARANTEE ANY ITERATION ORDERING

Yup, super unpredictable. Notice that I sort the filenames, which is important if the order of the files matters, i.e. for video frames or time dependent data collection. Be sure to put indices in your filenames though!