write to file

Chris Angelico rosuav at gmail.com
Sun May 6 01:17:57 EDT 2018


On Sun, May 6, 2018 at 3:10 PM, Sharan Basappa <sharan.basappa at gmail.com> wrote:
> On Saturday, 5 May 2018 21:47:33 UTC+5:30, Steven D'Aprano  wrote:
>> On Sat, 05 May 2018 08:45:39 -0700, Sharan Basappa wrote:
>>
>> > Thanks a lot. I have actually tried print with file handle as a
>> > parameter (the last option). I see that the file is created but nothing
>> > is logged.
>>
>> That could be a file buffer issue. Nothing will actually be written to
>> the disk until either the buffer is full, or you close the file. Try
>> calling fh.flush() from time to time, or use:
>>
>>     print(msg, file=fh, flush=True)
>>
>>
>> although things may be different on Windows. (For example, you may not be
>> able to open the file while it is still open in Python.)
>>
>>
>>
>> --
>> Steve
>
> Steve,
>
> I agree that flushing could be an issue but I assume when the program closes, buffered data should be  flushed even if I am explicitly flushing. This I did not see happening. Probably, I have to spend some additional time.
> --

Can you post a complete (but preferably small) example of a program
that creates a file but doesn't properly write to it?

ChrisA



More information about the Python-list mailing list