[Tutor] Question on Reading a Directory

Daniel Ehrenberg littledanehren at yahoo.com
Mon Oct 27 21:56:42 EST 2003


> Hi all,
> 
> I'm a very new Python programmer, and I want to get
> all the filenames 
> (Windows) into a list.  I have been unable to figure
> out how to do this and 
> don't see any functions in the library to do it.

What do you mean all of the filenames? All of the
filenames in a folder? All of they python files on
your computer? All of the files on your computer?
> 
> Your help is appreciated.
> 
> Also, just out of curiousity.... this is how I copy
> a file:
> 
> splash = select_splash()
> win32file.CopyFile(splash, '..\\dlgsplash.bmp',0)
> 
> and I was just wondering, if I need to do it this
> way (as it seems 
> un-portable).
> 
> Cheers,
> 
> Patrick

Yes, it is unportable. I just didn't download the
win32all extentions so that I wouldn't accidentally
write such unportable code. (If you need an IDE that's
better than Python but not the Win32 IDE, use the one
that comes with wxPython.) I would probably write the
code like this (but it wouldn't garbage collect
properly on Jython):

>>> infilepath = raw_input("input ")
>>> outfilepath = raw_input("output ")
>>> print >> file(outfilepath, 'w'),
file(infilepath).read()

There is probably a more efficient way to write it,
but I can't think of it. On my computer, it works
virtually instantaniously, so I don't really care if
it's as fast as with win32all.

Note: for most other cross-platform operations (that
would normally be platform-dependent), you would
usually use the os or sys modules.
Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/



More information about the Tutor mailing list