wasm-demo/demo/ermis-f/python_m/cur/1636

43 lines
1.3 KiB
Plaintext

From: akuchlin at cnri.reston.va.us (Andrew M. Kuchling)
Date: Tue, 27 Apr 1999 12:07:04 -0400 (EDT)
Subject: Directories
In-Reply-To: <3725DDD2.149DF47F@geocities.com>
References: <3725DDD2.149DF47F@geocities.com>
Message-ID: <14117.57115.810107.332251@amarok.cnri.reston.va.us>
X-UID: 1636
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