Directory in Windows

C

custard_pie

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?
 
K

Kent Johnson

custard_pie said:
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,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:
... 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:
... print file
...
f:/tutor\AllTheSame.py
f:/tutor\AppendTimes.bmp
f:/tutor\ArtOfWar.txt
etc...

http://www.jorendorff.com/articles/python/path/

Kent
 
C

custard_pie

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....
 
M

Marc 'BlackJack' Rintsch

custard_pie said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,237
Messages
2,571,189
Members
47,825
Latest member
XCCMilo924

Latest Threads

Top