469,923 Members | 1,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Directory in Windows

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?

Jul 19 '05 #1
3 1465
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
Jul 19 '05 #2
Here's my code
================================================== ======
filelist={}
def listFiles(self, dirName, filesInDir):
for fname in filesInDir:
if os.path.isfile(fname):
key = os.path.join(dirName, fname)
stats = os.stat(fname)
filelist[key] = (stats[stat.ST_MTIME], stats[stat.ST_SIZE])
os.path.walk(string.strip(self.path.get()), listFiles, None)
print filelist
================================================== =====
I change: if not os.path.isdir(fname) to if os.path.isfile(fname)
because some directories are not recognized as directory, and I get an
error message because os.stat is called with the directory as arg. But
even after I change it into isfile(). There are still some
errors,..some images in the subdirectories won't get printed...
Help please....

Jul 19 '05 #3
In <11**********************@l41g2000cwc.googlegroups .com>, custard_pie
wrote:
Here's my code
================================================== ======
filelist={}
def listFiles(self, dirName, filesInDir):
for fname in filesInDir:
if os.path.isfile(fname):
`fname` contains just the file name without the path to the file. So this
gives `False` for every file name except if there's a file with the same
name in the current working directory.
key = os.path.join(dirName, fname)
stats = os.stat(fname)
Same problem with `stat()`. Move the assignment to `key` up and use that
to check with `isfile()`/`isdir()` and `stat()`.
filelist[key] = (stats[stat.ST_MTIME], stats[stat.ST_SIZE])
os.path.walk(string.strip(self.path.get()), listFiles, None)
print filelist
================================================== =====


Ciao,
Marc 'BlackJack' Rintsch
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

By using this site, you agree to our Privacy Policy and Terms of Use.