finding an exact match of filenames

Discussion in 'Python' started by jimgardener, Oct 16, 2010.

  1. jimgardener

    jimgardener Guest

    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
    jimgardener, Oct 16, 2010
    #1
    1. Advertising

  2. jimgardener

    Peter Otten Guest

    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
    Peter Otten, Oct 16, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B.J.
    Replies:
    4
    Views:
    732
    Toby Inkster
    Apr 23, 2005
  2. Andy
    Replies:
    2
    Views:
    300
    John Harrison
    Sep 10, 2005
  3. Wayne Shu
    Replies:
    2
    Views:
    262
    James Kanze
    Jul 29, 2007
  4. Neelesh Bodas

    Exact Match and Identity conversion

    Neelesh Bodas, Aug 8, 2007, in forum: C++
    Replies:
    17
    Views:
    699
  5. Mr.SpOOn
    Replies:
    4
    Views:
    494
    Lawrence D'Oliveiro
    Nov 4, 2008
Loading...

Share This Page