Working with paths with spaces in NT

David LeBlanc whisper at oz.nospamnet
Thu Jun 7 00:42:43 EDT 2001


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.

TIA

Dave LeBlanc



More information about the Python-list mailing list