Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pythonic way to access each file in a list of directories

Tags:

python

I have working code that looks like this:

# Wow. Much nesting. So spacebar
if __name__ == '__main__:
  for eachDir in list_of_unrelated_directories:
      for eachFile in os.listdir(eachDir):
          if eachFile.endswith('.json'):
            # do stuff here

I'd like to know if there is a more elegant way of doing that. I would like to not have my code nested three layers deep like that, and if I could get this to a one-liner like

for each file that ends with .json in all these directories:
  # do stuff

That would be even more awesome. I also edited this to point out that the directories are not all in the same folder. Like you might be looking for .json files in your home folder and also your /tmp folder. So I'm not trying to move recursively through a single folder.

like image 249
Kevin Thompson Avatar asked Oct 21 '22 11:10

Kevin Thompson


1 Answers

The most Pythonic way is (in my opinion) to write a function that yields the files of a certain type and use it. Then your calling code is very clear and concise. Some of the other answers are very concise but incredibly confusing; in your actual code you should value clarity over brevity (although when you can get both that is, of course, preferred).

Here's the function:

import os

def files_from_directories(directories, filetype):
    """Yield files of filetype from all directories given."""
    for directory in directories:
        for file in glob.glob(os.path.join(directory, '*' + filetype))
            yield file

Now your calling code really is a one-liner:

# What a good one-liner!!!!
for json_file in files_from_directories(directories, '.json'):
    # do stuff

So now you have a one-liner, and a very clear one. Plus if you want to process any other type of file, you can just reuse the function with a different filetype.

like image 189
Cody Piersall Avatar answered Oct 23 '22 03:10

Cody Piersall