spaces at ends of filenames or directory names on Win32

rtilley rtilley at vt.edu
Mon Feb 27 15:44:01 EST 2006


This will at least allow me to ID folders that start with whitespace... 
from within Windows too :) yet I still cannot rename the folders after 
stripping the whitespace... attempting to gives an [Errno 2] No such 
file or directory. The strip seems to work right too according to the 
prints before and after.

import os
import os.path
import string

dirs =  os.listdir(os.getcwd())
path = os.getcwd()
##print path

for d in dirs:
     # If the first space in a folder name is whitespace.
     if d[0] in string.whitespace:
##        print d
         try:
             new_path = os.path.join(path, d.strip())
             old_path = os.path.join(path, d)
             print new_path
             print old_path
             os.renames(old_path, new_path)
         except Exception, e:
             print e
     else:
         pass



More information about the Python-list mailing list