[Tutor] Need help appending data to a logfile

Steven D'Aprano steve at pearwood.info
Fri Jun 28 15:42:33 CEST 2013


On 28/06/13 23:18, Matt D wrote:

> what if i did some thing like this i saw on stackoverflow:
>
> f = open("bigfile.txt", "w")

That clears any existing content of bigfile.txt, and opens it for writing. Do you intend to clear the content?


> for tempfile in tempfiles:

What is in tempfiles? A list of files opened for reading? Where does this list come from?


>      while True:
>          data = tempfile.read(65536)
>          if data:
>              f.write(data)
>          else:
>              break

This copies the content of each tempfile into bigfile.txt.


> could i make the 'logfile.txt. a tempfile?

I don't know. What do you mean by tempfile?


> and could the f.write be changed to f.append?

No, files do not have an append method. Open the file in append mode, then all writes will append to the end of the file.


-- 
Steven


More information about the Tutor mailing list