Mailing List Archive

Directories
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
that ...thanks a bundle.


Anton
Directories [ In reply to ]
On Tue, 27 Apr 1999 smoothasice@geocities.com wrote:
> 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
> that ...thanks a bundle.

fnmatch standard module pretty good on it. Full listsing could also be
generated by os.listdir().

> Anton

Oleg.
----
Oleg Broytmann National Research Surgery Centre http://sun.med.ru/~phd/
Programmers don't die, they just GOSUB without RETURN.
Directories [ In reply to ]
smoothasice@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