How can I get the names of the files in a directory?

Discussion in 'Python' started by Sara Fwd, Jan 15, 2005.

  1. Sara Fwd

    Sara Fwd Guest

    Can you guys also help me find a module that looks in
    a directory and print out the names of the files in there?



    __________________________________
    Do you Yahoo!?
    Yahoo! Mail - Helps protect you from nasty viruses.
    http://promotions.yahoo.com/new_mail
    Sara Fwd, Jan 15, 2005
    #1
    1. Advertising

  2. Sara Fwd

    .removethis. Guest

    Sara Fwd said the following on 1/15/2005 8:10 AM:
    > Can you guys also help me find a module that looks in
    > a directory and print out the names of the files in there?


    You can use glob:

    >>> import glob
    >>> from os.path import isfile
    >>> print filter(isfile, glob.glob('/tmp/*')) # can use patterns


    (will print a list of all files in the given directory, matching the
    given pattern)

    If you want to traverse a directory tree recursively, please take a look
    at this recipe:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/200131

    Thanks,
    --Kartic
    .removethis., Jan 15, 2005
    #2
    1. Advertising

  3. Sara Fwd

    Egor Bolonev Guest

    On Sat, 15 Jan 2005 15:16:02 GMT, .removethis.
    <"(.removethis.)kartic.krishnamurthy"@gmail.com> wrote:

    > Sara Fwd said the following on 1/15/2005 8:10 AM:
    >> Can you guys also help me find a module that looks in
    >> a directory and print out the names of the files in there?

    >
    > You can use glob:
    >
    > >>> import glob
    > >>> from os.path import isfile
    > >>> print filter(isfile, glob.glob('/tmp/*')) # can use patterns

    >
    > (will print a list of all files in the given directory, matching the
    > given pattern)
    >
    > If you want to traverse a directory tree recursively, please take a look
    > at this recipe:
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/200131


    import os, os.path

    def get_all_files(path):
    if len(path) > 0:
    if path[-1] == ':':
    path = path + '\\'
    try:
    for i in os.listdir(path):
    j = os.path.join(path, i)
    if os.path.isdir(j):
    for ii in get_all_files(j):
    yield ii
    else:
    yield j
    except:pass

    for i in get_all_files('c:\\'):
    print i
    Egor Bolonev, Jan 15, 2005
    #3
  4. På 15. jan 2005 kl. 16:16 skrev .removethis.:

    > >>> import glob
    > >>> from os.path import isfile
    > >>> print filter(isfile, glob.glob('/tmp/*')) # can use patterns


    Nice example of when filter() is better than list comprehension.

    [f for f in glob.glob("/tmp/*") if isfile(fi)]
    is a bit too verbose, the iteration is a totally uninteresting part
    here.

    --
    Stian Søiland
    Trondheim, Norway
    http://www.soiland.no/
    =/\=
    Stian Soiland, Jan 16, 2005
    #4
    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. =?Utf-8?B?QmFzIEhlbmRyaWtz?=

    Directory and files names in temporary asp.net directory

    =?Utf-8?B?QmFzIEhlbmRyaWtz?=, Jan 28, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    822
    Juan T. Llibre
    Jan 31, 2005
  2. news.microsoft.com
    Replies:
    1
    Views:
    831
    fd123456
    Feb 27, 2005
  3. ÖÜÕÑÌÎ
    Replies:
    1
    Views:
    709
    Rob Williscroft
    Dec 3, 2003
  4. fBechmann
    Replies:
    0
    Views:
    393
    fBechmann
    Jun 10, 2004
  5. iMath
    Replies:
    8
    Views:
    278
    emile
    Nov 13, 2012
Loading...

Share This Page