[Tutor] error in writelines
Luke Paireepinart
rabidpoobear at gmail.com
Sun Sep 10 00:08:58 CEST 2006
LL wrote:
> Hi All,
>
> I have a list containing 108 filenames. I want to open each file and
> write it to an output file, appending it to the previous write. I use
> the code below. Everything appears to work fine until I reach file
> 107. Only part of the file is written, and file 108 is not written at
> all. The program runs to completion (I get exit code 0). I debugged
> the problem and found that readlines() is reading all of file 107's
> contents but that writelines() is not writing all of file 107's
> contents. There are no strange characters where the write stops.
>
> Any advice would be greatly appreciated.
> Thanks,
> Lance
> -----------------------------------------------------------------
> fileobjectw = open(COMPILETRACEDIR + '\\IncludeCode.prg', 'w')
> for line in listext:
> line = string.strip(line)
> fileobjectr = open(line, 'r')
> sa = fileobjectr.readlines()
> fileobjectr.close()
> fileobjectw.writelines(sa)
> fileobjectw.close()
Sounds like maybe you should just do a binary read -> write.
like,
outputfile = file(os.path.join(COMPILETRACEDIR,'IncludeCode.prg'),'wb')
tmp = file('filenames.txt','r')
filenames = [a.strip() for a in tmp.readlines()]
tmp.close()
for filename in filenames:
inputfile = file(filename,'rb')
outputfile.write(inputfile.read())
inputfile.close()
outputfile.close()
I think that should work.
I don't have the files you're running this all on, though.
It might help if you gave us the text of a successful file and of 107.
HTH,
-Luke
More information about the Tutor
mailing list