Working with paths with spaces in NT
David LeBlanc
whisper at oz.nospamnet
Wed Jun 13 15:58:12 EDT 2001
[This followup was posted to comp.lang.python and a copy was sent to the
cited author.]
FWIW, here's my latest iteration. I'm rather proud of it as a first hack
in Python beyond the "enter a few lines and run a few programs" level of
investigation. I'd appreciate any critique, especially wrt to better
python idioms for doing things (like the spaces/tabs kiudge noted below).
This reminded me so much of my beloved Smalltalk without some of it's
kinks!
------------------------------------------------------------
#BEWARE of line wraps!
#NOTE: Set tabstops = 2 for best viewing!
import os
import dircache
import re
dirtotal = 0
filetotal = 0
doctotal = 0
basepath = "L:/languages/python"
#WRAP ERROR next line!
tabs = "
"
extensions = ["exe", "tar", "tgz", "gz", "zip", "py", "c"]
docextensions = ["html", "htm", "pdf", "ps"]
filetree = {}
def buildtree(path, depth, tree):
global dirtotal, filetotal, doctotal, tabs, extensions,
docextensions, filetree
base = ""
ext = ""
tree["filelist"] = []
tree["dircount"] = 0
tree["filecount"] = 0
currentdepth = depth
depth += 1
spaces = tabs[0:(depth * 2)] # kludge - there's a better way,
# but i'm not clear on how to use it.
os.chdir(path)
for name in dircache.listdir("."):
if os.path.isdir(name) and name != "CVS":
print spaces + name
tree["dircount"] += 1
tree[name] = {}
buildtree(name, depth, tree[name])
else:
base, ext = os.path.splitext(name)
if ext[1:] in docextensions:
doctotal += 1
elif ext[1:] in extensions:
tree["filecount"] += 1
tree["filelist"].append(name)
currentspaces = tabs[0:(currentdepth * 2)]
print currentspaces + path + ": Dirs:", tree["dircount"], "Files:",
tree["filecount"]
filetotal += tree["filecount"]
dirtotal += tree["dircount"]
os.chdir("..")
return tree
if __name__ == '__main__':
print basepath
filetree = buildtree(basepath, 1, filetree)
print "Total Dirs:", dirtotal, "Total Files:", filetotal
print "Total Doc Files:", doctotal
----------------------------------------------------------------------
Mats, you must have missed my apology to Tim the next day - who knows why
cats arch their backs at shadows ;-) I also did get the hint about
dir(".") and chdir("..").
Dave LeBlanc
In article <3b279eaf.5984935 at news.laplaza.org>, xyzmats at laplaza.org
says...
> Tim wrote:
>
> >> If you look at your output very carefully, you'll discover this has
> nothing
> >> to do with spaces. After all, you chdir'ed to
> >>
> >> A Gentle Introduction to Ted Nelson's ZigZag Structure_files
> >>
> >> just fine. If that's not enough of a hint, try this starting in
> some deep
> >> directory tree that doesn't have any spacey names.
> >>
> >> don't-use-relative-paths-unless-you're-sure-you-know-where-you-
> >> are-ly y'rs - tim
>
> And Dave replied:
>
> >Thank you Tim for the warm and friendly help without a hint of
> >condescension - i'm sure it really encourages people new to python
> > and even more those new to programming.
>
>
> I missed what it was raised your hackles in Tim's comment.
>
> He pointed out that it wasn't a space problem since you've been in a
> directory with spaces already. The more obscure reference is that you
> never came out of that directory before trying to go on to the next
> one.
>
> You need an os.chdir('..') somewhere.
>
> -- mats
>
> Mats Wichmann
>
> (Anti-spam stuff: to reply remove the "xyz" from the
> address xyzmats at laplaza.org. Not that it helps much...)
>
More information about the Python-list
mailing list