get directory and file names

A

Alchemist

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.
 
M

Marc 'BlackJack' Rintsch

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
 
B

brad

Alchemist said:
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))
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top