Permissions or mutable objects... Read only file contents template to write only?
Hans Nowak
ivnowa at hvision.nl
Mon Feb 12 07:56:43 EST 2001
Gavin Tomlins wrote:
> #open files for various operations
> fileHdr = open(cTemplateHdr, "r")
> fileFooter = open(cTemplateFooter, "r')
> fileOutput = open(cOutput, "w")
>
> cTempStr = ""
> cTempStr = fileHdr.readlines()
>
> fileOutput.write(cTempStr)
>
> at this point I am presented with a type error indicating that I'm trying to
> write a 'read buffer'.
fileHdr.readlines() returns a list. So, you are trying to write a list
of strings to fileOutput, while the .write method wants a string.
You can loop over the list, writing it line by line:
for line in cTempStr:
fileOutput.write(line)
or use the .writelines method:
fileOutput.writelines(cTempStr)
Maybe you wanted to read only one line from the input file; in that case
you can use the .readline method.
> What is the process for rectifying this problem. Any assistance, reference
> would be greatly appreciated. I've searched the Python documentation but was
> unable to find appropriate documentation. If possible, could people cite the
> reference for my understanding. I can only put it down to lack of comprehension
> or poor choice of search criteria on my behalf.
See the Python library reference, 2.1.7.9.
HTH,
More information about the Python-list
mailing list