Autarchy of the Private Cave

Science, Society, Programming and Hobbies

  • Exits

  • Categories

  • Archives

  • Visitors' track

    Locations of visitors to this page
  • Tags list

Python: iterate (and read) all files in a directory (folder)

12th August 2007

I found a sample of Python code to iterate through all the files within the specified folder (directory), with ability to use wildcards (*, ?, and [ ]-style ranges). Below is a portion of code from a working script:

PYTHON:
  1. import os, glob
  2.  
  3. path = 'sequences/'
  4. for infile in glob.glob( os.path.join(path, '*.fasta') ):
  5.   print "current file is: " + infile
  6.   # for python 3.0 and above, print is now a function print():
  7.   # print("current file is: " + infile)

Thanks Dt for mentioning print being promoted from a statement to a function.

Clearly, the only reason to use that 'os.path.join()' part is to make the script cross-platform-portable, as different systems use different path separators, and hard-coding path separator would stop the script from executing under a different OS.

Python docs mention that there is also iglob(), which is an iterator and thus working on directories with way too many files it will save memory by returning only single result per iteration, and not the whole list of files - as glob() does.

Share This

4 Responses to “Python: iterate (and read) all files in a directory (folder)”

  1. seun Says:

    This stuff isnt working on my windows system

  2. Bogdan Says:

    it should. you might want to modify the example to suit your exact needs - this example assumes there is 'sequences' folder with at least one .fasta file - if that's not true, then - evidently - you are not going to see this script work, be it windows or linux.

  3. Dt Says:

    works just fine for me, only important change to the code that i had to make was turning print into a function because im using python 3.0, i also set it to read files with *all* extensions.

    import os, glob

    path = 'insert your own path you lazy bastards '

    for infile in glob.glob( os.path.join(path, '*.*') ):
    print("current file is: " + infile)

  4. Bogdan Says:

    Dt, thanks, I've updated the code.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
Close
E-mail It