Working with paths with spaces in NT

Tim Peters tim.one at home.com
Thu Jun 7 01:03:37 EDT 2001


[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





More information about the Python-list mailing list