[Tutor] Need help appending data to a logfile
Matt D
md123 at nycap.rr.com
Fri Jun 28 16:10:15 CEST 2013
On 06/28/2013 09:42 AM, Steven D'Aprano wrote:
> 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.
>
>
just to be clear here.
this how i open and the file for logging data:
# open a file named
"logfile.txt" in "a" append mode;
self.logfile = open('logfile.txt', 'a')
and i have a loop that writes 8 comma separated values in row with this:
self.logfile.write('%s,'%(str(f))
currently this is how the user can open a file from the UI:
#open file dialog -----------------------------
def openFile(self, evt):
with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
"*.*", wx.OPEN) as dlg:
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
mypath = os.path.basename(path)
#with open(mypath, "a") as f:
but i havnt figured a way to get what is in the 'logfile.txt' into the
file the user opens with the file dialog.
so. . . i was looking at the documentation for tempfile, stuff like
'tempfile.TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[,
prefix='tmp'[, dir=None]]]]])'. I was thinking that if i could have my
program log data to a temp file (in place of 'logfile.txt') on each run
and then the tempfile would be deleted. but before the tempfile was
deleted on program stop, the contents of the tempfile could be appended
into the file the user opened from the UI.
So "bigfile.txt" would be the file the user opens with the file dialog,
here it would be 'mypath'. and the 'logfile.txt' would be the temporary
file, i would have to replace in my program 'logfile.txt' with 'tempfile'?
Not sure if this doable and if so how i would go about writing this code.
More information about the Tutor
mailing list