questions
Hans Nowak
hnowak at cuci.nl
Mon Oct 2 17:24:49 EDT 2000
On 2 Oct 00, at 14:09, Xiaoxia Dong wrote:
> i am working on one of the project which needs using python, i am trying
> to read a directory and list all
> the files there according to the directory tree. I am thinking to make
> the directory name bold and trying to
> have some format like a2ps in unix. I am just writing the output to a
> text file. I don't use any graphical interface. Can anybody help me with
> this?
I'm not sure what you mean here... if you want all files in a certain
directory, you can use glob:
>>> import glob
>>> glob.glob("c:/PythonWin/*.*")
['c:/PythonWin\\UNWISE.EXE', 'c:/PythonWin\\python.exe',
... etc ... 'c:/PythonWin\\test1.py', 'c:/PythonWin\\logfile.txt',
'c:/PythonWin\\xref-results.html']
If you want a files in all subdirectories in a tree, you should use
os.path.walk:
>>> import os
>>> def visit(arg, dirname, names):
# just an example
print dirname, ":"
print names
>>> os.path.walk("c:/foo/bar/", visit, 0)
# should print list of subdirectories and their files
Hope this will get you started,
--Hans Nowak (zephyrfalcon at hvision.nl)
You call me a masterless man. You are wrong. I am my own master.
May a red dragon make a horoscope about your arms!
More information about the Python-list
mailing list