Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > finding an exact match of filenames

Reply
Thread Tools

finding an exact match of filenames

 
 
jimgardener
Guest
Posts: n/a
 
      10-16-2010
I am trying to code a function that takes a directory name and a name
string and try to return a list of fully qualified names of all files
in the directory having an exact match of name.

example,
I have these files in the directory,
'googlegroup_python__111.txt',
'googlegroup_python__112.txt',
'googlegroup_python_django__111.txt',
'googlegroup_python_django__112.txt',
'googlegroup_python_grok__111.txt',

When I call
get_filenames('mydir', 'googlegroup_python' )

I expect to get a list like
['/mydir/googlegroup_python__111.txt', '/mydir/
googlegroup_python__112.txt']

but not the other three.

I was careless when I coded the method as

def get_filenames(dirname,filenamestr):
match_filenames=[dirname+os.sep+x for x inos.listdir(dirname) if
x.startswith(filenamestr)]
return match_filenames

because this would return all 5 of the above filenames.I think reg
expression maybe the way to do this..but not very sure as to how I can
do it..

Any help or suggestions greatly appreciated
thanks
jim
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-16-2010
jimgardener wrote:

> I am trying to code a function that takes a directory name and a name
> string and try to return a list of fully qualified names of all files
> in the directory having an exact match of name.
>
> example,
> I have these files in the directory,
> 'googlegroup_python__111.txt',
> 'googlegroup_python__112.txt',
> 'googlegroup_python_django__111.txt',
> 'googlegroup_python_django__112.txt',
> 'googlegroup_python_grok__111.txt',
>
> When I call
> get_filenames('mydir', 'googlegroup_python' )
>
> I expect to get a list like
> ['/mydir/googlegroup_python__111.txt', '/mydir/
> googlegroup_python__112.txt']
>
> but not the other three.
>
> I was careless when I coded the method as
>
> def get_filenames(dirname,filenamestr):
> match_filenames=[dirname+os.sep+x for x inos.listdir(dirname) if
> x.startswith(filenamestr)]
> return match_filenames
>
> because this would return all 5 of the above filenames.I think reg
> expression maybe the way to do this..but not very sure as to how I can
> do it..
>
> Any help or suggestions greatly appreciated
> thanks
> jim


Try the glob module first:

>>> import os, glob
>>> print "\n".join(os.listdir("mydir"))

googlegroup_python_django__111.txt
googlegroup_python__112.txt
googlegroup_python_grok__111.txt
googlegroup_python_django__112.txt
googlegroup_python__111.txt

>>> print "\n".join(glob.glob("mydir/googlegroup_python__???.txt"))

mydir/googlegroup_python__112.txt
mydir/googlegroup_python__111.txt

If glob() isn't selective enough you may have to resort to regular
expressions.

Peter
 
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
Exact match with regular expression Mr.SpOOn Python 4 11-04-2008 08:59 PM
Exact Match and Identity conversion Neelesh Bodas C++ 17 08-14-2007 11:49 PM
some problems about exact match in the overload resolution. Wayne Shu C++ 2 07-29-2007 07:21 PM
conversion, promotion, exact match Andy C++ 2 09-10-2005 01:05 PM
problem with filenames, Filenames and FILENAMES B.J. HTML 4 04-23-2005 08:13 PM



Advertisments