get directory and file names

Discussion in 'Python' started by Alchemist, Jul 31, 2007.

  1. Alchemist

    Alchemist Guest

    I am working with Python 2.5 on Windows XP (SP2).

    How can I traverse a folder, loop through a list of files and get
    their file name and extension in an elegant, pythonic way?

    Thank you.
     
    Alchemist, Jul 31, 2007
    #1
    1. Advertising

  2. On Tue, 31 Jul 2007 07:23:06 -0700, Alchemist wrote:

    > I am working with Python 2.5 on Windows XP (SP2).
    >
    > How can I traverse a folder, loop through a list of files and get
    > their file name and extension in an elegant, pythonic way?


    Take a look at the `os` and the `os.path` modules. In particular
    `os.listdir()`, `os.path.isfile()` and `os.path.splitext()`.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jul 31, 2007
    #2
    1. Advertising

  3. Bruno Desthuilliers, Jul 31, 2007
    #3
  4. Alchemist

    brad Guest

    Alchemist wrote:
    > I am working with Python 2.5 on Windows XP (SP2).
    >
    > How can I traverse a folder, loop through a list of files and get
    > their file name and extension in an elegant, pythonic way?
    >
    > Thank you.
    >

    try this:

    for root, dirs, files in os.walk('.'):
    for f in files:
    print os.path.splitext(os.path.join(root,f))
     
    brad, Jul 31, 2007
    #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:
    854
    Juan T. Llibre
    Jan 31, 2005
  2. Ferman
    Replies:
    0
    Views:
    485
    Ferman
    Aug 13, 2003
  3. fBechmann
    Replies:
    0
    Views:
    408
    fBechmann
    Jun 10, 2004
  4. Maciej
    Replies:
    2
    Views:
    402
    Greg Comeau
    Oct 18, 2005
  5. iMath
    Replies:
    8
    Views:
    304
    emile
    Nov 13, 2012
Loading...

Share This Page