Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie wants to compile python list of filenames in selected directories

Reply
Thread Tools

newbie wants to compile python list of filenames in selected directories

 
 
anthonyberet
Guest
Posts: n/a
 
      02-06-2005
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
 
Reply With Quote
 
 
 
 
Greg Krohn
Guest
Posts: n/a
 
      02-06-2005
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


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
 
Reply With Quote
 
 
 
 
John J. Lee
Guest
Posts: n/a
 
      02-06-2005
Greg Krohn <(E-Mail Removed)> writes:
> 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
 
Reply With Quote
 
M.E.Farmer
Guest
Posts: n/a
 
      02-07-2005
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,
M.E.Farmer

 
Reply With Quote
 
Caleb Hattingh
Guest
Posts: n/a
 
      02-07-2005
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




On Sun, 06 Feb 2005 18:29:34 +0000, anthonyberet <(E-Mail Removed)> 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


 
Reply With Quote
 
anthonyberet
Guest
Posts: n/a
 
      02-08-2005
M.E.Farmer wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
selected=selected in the center of option list John HTML 5 03-01-2010 08:30 PM
Macro to indicate directories in filenames raphfrk C Programming 8 03-29-2008 04:04 PM
Comparing filenames in different directories Deepu Perl Misc 10 03-16-2007 04:17 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
problem with filenames, Filenames and FILENAMES B.J. HTML 4 04-23-2005 08:13 PM



Advertisments