Working with paths with spaces in NT

David LeBlanc whisper at oz.nospamnet
Thu Jun 7 21:37:32 CEST 2001


In article <mailman.991890250.31525.python-list at python.org>, 
tim.one at home.com says...
> [David LeBlanc]
> > import os
> > import dircache
> > import re
> >
> >
> > def getDirs(path, tabs):
> > 	dirlist = []
> > 	os.chdir(path)
> > 	for dir in dircache.listdir("."):
> > 		if os.path.isdir(dir):
> > 			dirlist.append(dir)
> > 	print dirlist
> > 	for dir in dirlist:
> > 		print dir
> > 		getDirs(dir, tabs + 1)
> >
> > getDirs("L:/languages/python", 1)
> >
> > This will report: [k:/Python21/Projects]% python FileList.py
> > ['Apps', 'CVS', 'Database', 'Distributions', 'Docs', 'Tools', 'libs']
> > Apps
> > ['ThoughtStream', 'XML', 'moop']
> > ThoughtStream
> > ["A Gentle Introduction to Ted Nelson's ZigZag Structure_files",
> > 'ThoughtStream Design notes_fil
> > es', "ThoughtStream User's Guide_files", 'XML Topic Maps (XTM) 1_0
> > errata_files', 'XML Topic Map
> > s (XTM) 1_0_files', 'XML Topic Maps (XTM) Processing Model 1_0_files']
> > A Gentle Introduction to Ted Nelson's ZigZag Structure_files
> > []
> > ThoughtStream Design notes_files
> > Traceback (most recent call last):
> >   File "k:\python21\projects\FileList.py", line 17, in ?
> >     getDirs("L:/languages/python", 1)
> >   File "k:\python21\projects\FileList.py", line 15, in getDirs
> >     getDirs(dir, tabs + 1)
> >   File "k:\python21\projects\FileList.py", line 15, in getDirs
> >     getDirs(dir, tabs + 1)
> >   File "k:\python21\projects\FileList.py", line 15, in getDirs
> >     getDirs(dir, tabs + 1)
> >   File "k:\python21\projects\FileList.py", line 8, in getDirs
> >     os.chdir(path)
> > OSError: [Errno 2] No such file or directory: 'ThoughtStream Design
> > notes_files'
> >
> > How do I process directory names with spaces? I looked through the python
> > doc, but didn't see anything.
> 
> 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
> 
> 
> 

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 
evem more those new to programming.

probably-been-programming-for-longer-then-you've-been-alivedly-yours...

Dave LeBlanc

P.S. In case you're wondering, 30 years as of last month.



More information about the Python-list mailing list