471,348 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

get directory and file names

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.

Jul 31 '07 #1
3 19785
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
Jul 31 '07 #2
Alchemist a écrit :
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?
http://docs.python.org/lib/module-os.path.html
http://docs.python.org/lib/os-file-dir.html
(=os.walk)

Jul 31 '07 #3
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))

Jul 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by LELAND PRINCE | last post: by
6 posts views Thread by Mark | last post: by
2 posts views Thread by Jacob Lyles | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.