anthonyberet wrote:
Hi, I am new at Python, and very rusty at the one language I was good
at, which was BASIC.
I want to write a script to compare filenames in chosen directories,
on
windows machines. Ideally it would compose a list of strings of all
the
filenames in the directories, and those directories would be chosable
by
the user of the script.
I am quite happy to do my own legwork on this , I realise it is
simple
stuff, but can anyone point me in the right direction to start?
Thanks
Cool! I like your attitude ;) Others have given you a good start with os. In case you don't know os has a great many path manipulation methods. Always try to use them so you can insulate yourself from cross platform path nightmares. A few of the highlights are:
### split a path py> parts = os.path.split(r'c:\windows\media\ding.wav') py> print parts ('c:\\windows\\media', 'ding.wav')
### join a path and part and do it right on any platform py> path = os.path.join('c:\\windows\\media', 'ding.wav') 'c:\\windows\\media\\ding.wav'
### get basename of the file py> base = os.path.basename('c:\\windows\\media\\ding.wav') py> print base 'ding.wav'
Plus many more, be sure to study the os module if you are doing any path manipulations.
Ok and now for something sorta different( alright, I was really bored )
. def glob_dir(dir): . """Return a list of *.py* (.py, .pyc, .pyo, .pyw) . files from a given directory. . """ . import glob, os . # Get a list of files that match *.py* . GLOB_PATTERN = os.path.join(dir, "*.[p][y]*") . pathlist = glob.glob(GLOB_PATTERN) . return pathlist . . def list_dir(dir): . ''' Another way to filter a dir ''' . import os . pathlist = os.listdir(dir) . # Now filter out all but py and pyw . filterlist = [x for x in pathlist . if x.endswith('.py') . or x.endswith('.pyw')] . return filterlist hth,
Thanks to everyone who responded - I feel on my way now, just being able to tap 'os python' and 'lisdir python' into Google has netted me plenty of reading material - but I would have taken a long time to find that out without your tips!
I will surely come back, but not until I get I get stuck :)
--
http://mail.python.org/mailman/listinfo/python-list