get directory and file names

brad byte8bits at gmail.com
Tue Jul 31 11:14:14 EDT 2007


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))






More information about the Python-list mailing list