Directories
Andrew M. Kuchling
akuchlin at cnri.reston.va.us
Tue Apr 27 12:07:04 EDT 1999
smoothasice at geocities.com writes:
>Hi, i'm curious if there is a python command or set of commaneds that
>could be used like the dir() command in dos prompt. I am trying to
>write a program in python that would allow me to list the files of a
>certain data type in a few directories but I need to know how I would do
There are a few ways of doing this.
* os.listdir(path) returns a list containing the names of all
the files in the directory specified by path.
>>> import os
>>> os.listdir('.')
['Group.java', 'Group.class', 'User.java', ... ]
So you can do os.listdir('C:/whatever/sub/dir/ectory'), and
then loop over each filename with a for loop.
* The glob module returns a list of filenames matching a
wildcard pattern.
>>> import glob
>>> glob.glob('*.java')
['Group.java', 'User.java', 'GroupWrapper.java', ... ]
--
A.M. Kuchling http://starship.python.net/crew/amk/
The chaos of our lives suited me; I don't think I wanted it to end.
-- Tom Baker, in his autobiography
More information about the Python-list
mailing list