473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1544
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jay Chan | last post by:
We have just installed a SQL Server 2000 (SP 3A) onto a computer that has Windows-2003 Server on it. Now, we cannot get access to that database server from other computers. Seem like this may be an...
2
by: Jay Chan | last post by:
We have just installed a SQL Server 2000 (SP 3A) onto a computer that has Windows-2003 Server on it. Now, we cannot get access to that database server from other computers. Seem like this may be an...
5
by: Bas Hendriks | last post by:
Has anyone any idea how asp.net find it's files back after compiling them to the temporary asp.net directory? I found on numerous webpages that the directorynames are chosen random but cannot find...
10
by: Hriday | last post by:
Hi there, Please help me..It is urgent This is Hriday, working on windows authentication with Active Directory... My requirment is when a user sends a request to my web Applicatoin I want to...
18
by: troywalker | last post by:
I am new to LDAP and Directory Services, and I have a project that requires me to authenticate users against a Sun Java System Directory Server in order to access the application. I have found...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.