By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

get directory and file names

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.