Recent Comments

Kevin | When I moved to a ...
Parameswaran Bose | Please get this site back ...
Mike Klaene | Hi Kevin, I have previously ...
thad | Please get this site back ...

Syndicate

Latest Posts
Latest Comments

Archives

Dec 2007
May 2008

A Pythonic way to iterate a set of files

# May 09, 2008 at 10:05 p.m. by: kevin (0) Comments

In this post, I present a Pythonic approach for Python developers to easily iterate through a set of files, all the files in a folder or simply a single file. You might find yourself having to write over and over the same code that would check if a given string represents a file or a folder. If it's the case of a folder, the code would get the list of files in that folder and put the result in a list. Then finally, you would loop through that list of files to do some kind of processing on that file. You might also need to check if a file name has the right extension or starts with a certain pattern before processing it. If all that sounds repetitive and not elegant at all, I think you'll agree the following approach would be elegant and Pythonic.

tagged to: python