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:
- import os
- import glob
- path = 'sequences/'
- for infile in glob.glob( os.path.join(path, '*.fasta') ):
- print "current file is: " + infile
If you do not need wildcards, then there is a simpler way to list all items in a directory:
- import os
- path = 'sequences/'
- listing = os.listdir(path)
- for infile in listing:
- 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.
December 23rd, 2008 at 11:38
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)
December 23rd, 2008 at 13:21
Dt, thanks, I've updated the code.
May 14th, 2009 at 3:52
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: "))
May 18th, 2009 at 22:25
Thankyou very much...
This was exactly what I was looking for!
November 24th, 2009 at 3:38
marvellous
December 7th, 2009 at 2:11
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
January 3rd, 2010 at 20:14
Is there a way to change this script so that it also runs through sub-directories under the given path name?
January 3rd, 2010 at 21:07
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.
January 11th, 2010 at 22:07
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.
January 11th, 2010 at 23:44
Dan,
script below seems to work perfectly for me:
Basically, I've changed the '*.*' wildcard to just '*'.
January 12th, 2010 at 4:43
Ahh... My *.* as opposed to a * had it so it wasn't looking at folders, thus the problem. Thanks again!
November 30th, 2010 at 1:47
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.
January 26th, 2011 at 21:05
Thanks. This snippet helped a bunch.
May 13th, 2011 at 13:51
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
May 13th, 2011 at 14:20
I found it:
dirList=os.listdir(path)
dirList.sort()
for fname in dirList:
print( fname)
August 10th, 2011 at 14:54
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.
September 14th, 2011 at 11:28
thank you very much for your explaining. I get a problem when try to list file or directory in Python. You solve my problem
October 13th, 2011 at 14:40
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??