newbie wants to compile python list of filenames in selected directories

A

anthonyberet

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
 
G

Greg Krohn

anthonyberet said:
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

os.listdir does almost exactly what you are asking for. For a little
more flexibility, you can use os.path.walk, although it's not quite as
newb friendly.

-greg
 
J

John J. Lee

Greg Krohn said:
anthonyberet wrote: [...]
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.
[...]

os.listdir does almost exactly what you are asking for. For a little
more flexibility, you can use os.path.walk, although it's not quite as
newb friendly.

-greg

os.walk is perhaps newbie-friendlier than os.path.walk.

Or perhaps not: not having read the tutorial &c. for some time, I
don't know how good they are on generators...


John
 
M

M.E.Farmer

anthonyberet said:
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,
M.E.Farmer
 
C

Caleb Hattingh

Hi Anthony

Here is some stuff to get you started (this is from memory, I'm not
checking it, but should be mostly helpful):

***
import os

os.chdir('C:\My Documents') # Can use this to jump around to different
folders
fileNames = os.listdir('.') # Checks the now current folder
namesToMatch = ['readme.txt','readme.doc'] # Buncha names to find

for item in fileNames: # check every filename
if item in namesToMatch: # is this item in the required list?
print 'Match found: '+item # if you found one, say so.
***

Hope this helps.
Caleb
 
A

anthonyberet

M.E.Farmer said:
anthonyberet said:
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,109
Latest member
JanieMalco
Top