Directory in Windows
Kent Johnson
kent37 at tds.net
Sat Apr 30 13:51:52 EDT 2005
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
More information about the Python-list
mailing list