Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

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

    12th August 2007

    To iterate through all the files within the specified directory (folder), with ability to use wildcards (*, ?, and [ ]-style ranges), use the following code snippet:

    PYTHON:
    1. import os
    2. import glob
    3.  
    4. path = 'sequences/'
    5. for infile in glob.glob( os.path.join(path, '*.fasta') ):
    6.     print "current file is: " + infile

    If you do not need wildcards, then there is a simpler way to list all items in a directory:

    PYTHON:
    1. import os
    2.  
    3. path = 'sequences/'
    4. listing = os.listdir(path)
    5. for infile in listing:
    6.     print "current file is: " + infile

    print was promoted from a statement to a function in Python 3 (use print(infile) instead of print infile).

    One should use 'os.path.join()' part to make the script cross-platform-portable (different OS 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

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

    1. 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)

    2. Bogdan Says:

      Dt, thanks, I've updated the code.

    3. Mike Says:

      import os, glob

      def dir(path):
      for infile in glob.glob( os.path.join(path) ):
      print "current file is: " + infile

      path = dir(raw_input("Enter the path: "))

    4. Ferralll Says:

      Thankyou very much...
      This was exactly what I was looking for!

    5. Richard Says:

      marvellous

    6. Kris Says:

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

      #lists all files in directory script is in

    7. Dan Says:

      Is there a way to change this script so that it also runs through sub-directories under the given path name?

    8. Bogdan Says:

      Make that code into a function - e.g. scan_dirs(path) - and add a single line of code to it (pseudocode below):

      if os.path.isdir(infile): scan_dirs(infile)

      This will do exactly what you want.

    9. Dan Says:

      Bogdan,

      Thanks for the help. I'm still not getting the code to look at the directories within the path. Here's my code, it still only looks at the files under the initial path.

      
      def scandirs(path):
          for currentFile in glob.glob( os.path.join(path, '*.*') ):
              if os.path.isdir(currentFile):
                  scandirs(currentFile)
              print "processing file: " + currentFile
      scandirs('XML/')
      

    10. Bogdan Says:

      Dan,

      script below seems to work perfectly for me:

      
      import os, glob
      def scandirs(path):
          for currentFile in glob.glob( os.path.join(path, '*') ):
              if os.path.isdir(currentFile):
                  print 'got a directory: ' + currentFile
                  scandirs(currentFile)
              print "processing file: " + currentFile
      scandirs('Desktop')
      

      Basically, I've changed the '*.*' wildcard to just '*'.

    11. Dan Says:

      Ahh... My *.* as opposed to a * had it so it wasn't looking at folders, thus the problem. Thanks again!

    12. Bill Tate Says:

      Is there a way to also do thiw in Windows? What I need to do is
      process every *.txt file in a directory, one at a time, inside
      a Python script.

    13. Rommel Says:

      Thanks. This snippet helped a bunch.

    14. Stefan Says:

      Is there a possibility to list the files in order, by name ?
      For example :
      /path/file01.txt
      /path/file02.txt
      ..............

      If I use the codes you presented here i get scrambled order

    15. Stefan Says:

      I found it:

      dirList=os.listdir(path)

      dirList.sort()

      for fname in dirList:
      print( fname)

    16. ablaze Says:

      Hi...
      I am working in ubuntu. I have a bunch of commands (say 10 commands like cmd1, cmd2, cmd3..............cmd10)
      I want to write a python script, which can achive the following:

      It should traverse through the directory structure and apply a command at particular directory path.
      The location and the commands are already known to me.

      /local/mnt/myspace/sample1$ cmd1
      /local/mnt/myspace/sample2$ cmd2
      /local/mnt/myspace$ cmd3
      /local/mnt$cmd4
      /local/mnt/myspace/sample9$ cmd 8
      /local/mnt/myspace/sample3$ dmd10

      can someone please provide the script as I am not event a beginner in python.

    17. toto Says:

      thank you very much for your explaining. I get a problem when try to list file or directory in Python. You solve my problem :)

    18. born Says:

      hi ...
      I have been messing around with a python program to browse through images in a directory and display it in a canvas.can anybody help??

    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>