Re: Directory in Windows
- From: Kent Johnson <kent37@xxxxxxx>
- Date: Sat, 30 Apr 2005 13:51:52 -0400
custard_pie wrote:
Hi,..I tried to list files in a tree directory using os.path.walk. To avoid dirnames fromm being listed i use the os.path.isdir method. However, when isdir encounters directories that use spaces in their name e.q My Documents it doesn;t recognize them as directories.. Is there any solution to this,..pertaining that I want to keep the naming of my directories?
That seems unlikely. For example, >>> os.path.isdir(r'D:\My Documents') True
Can you show the code?
Alternatively you might try os.walk() which is a bit easier to use than os.path.walk() and it gives you separate lists for files and directories:
>>> import os
>>> for dirpath, dirnames, filenames in os.walk('f:/tutor'):
... for file in filenames:
... print os.path.join(dirpath, file)
...
f:/tutor\AllTheSame.py
f:/tutor\AppendTimes.bmp
ecc...Or even easier, use jorendorff's path module which has a walkfiles() method that iterates over files directly and gives you path objects to work with:
>>> import path
>>> for file in path.path('f:/tutor').walkfiles():
... print file
...
f:/tutor\AllTheSame.py
f:/tutor\AppendTimes.bmp
f:/tutor\ArtOfWar.txt
etc...http://www.jorendorff.com/articles/python/path/
Kent .
- Follow-Ups:
- Re: Directory in Windows
- From: custard_pie
- Re: Directory in Windows
- References:
- Directory in Windows
- From: custard_pie
- Directory in Windows
- Prev by Date: [Py Windows] User Directory Path
- Next by Date: Re: Directory in Windows
- Previous by thread: Directory in Windows
- Next by thread: Re: Directory in Windows
- Index(es):