How to write to tempfile, remove whitespace, and copy to source ..?

Randolph MacKenzie rmack at eznet.net
Sun Jan 21 12:30:32 EST 2001


I want a module that will remove the extra whitespace from all text files in
a target directory. I want to use the tempfile command (on a Windoze2000
box). I guess I'm having trouble opening the tempfile and appending to it...
and then, after processing it, I need to copy (overwrite) it to source file.

Any suggestions ?


# X2space.py removes all extra whitespace from textfiles

target = "E:\\text\\"
import glob, fileinput, os, shutil, string, tempfile
filelist = glob.glob(target+"*.TXT")

for fl in filelist:
 L3 =[];
 tmp = tempfile.TemporaryFile('a')
 #tmpfile = open(tmpflname,'a')
 for line in fileinput.input(fl):
  tmp.write(string.strip(string.join(string.split(line))))
 tmp.close()
 shutil.copyfile(tmp,fl)







More information about the Python-list mailing list