Failure of spyder/python to flush file write buffer

24 views
Skip to first unread message

Stephen Derenzo

unread,
Sep 14, 2021, 12:01:09 AM9/14/21
to anac...@anaconda.com, Martin Boswell

Anaconda community,

I am using Spyder 5.05 on a 2019 MacBook Pro with OS 11.5.2 (Big Sur). I find that the python file write command does not complete the file, even when using the flush command. I attach the .py file and a typical output file that is missing the last 13 lines.

Thanks,
Stephen
--
-----------------

Stephen E. Derenzo
Senior Physicist
Lawrence Berkeley National Laboratory
Berkeley, CA 94720
sede...@lbl.gov
Test_out.txt
output_flush_test.py

David Wilkins

unread,
Sep 14, 2021, 2:13:44 PM9/14/21
to anac...@anaconda.com
Try outfile.flush() and outfile.close()



> On Sep 13, 2021, at 9:00 PM, Stephen Derenzo <sede...@lbl.gov> wrote:
>
> <output_flush_test.py>

Stephen Derenzo

unread,
Sep 14, 2021, 3:56:22 PM9/14/21
to anac...@anaconda.com
David,

Thanks. Adding () after the flush and close calls was essential. Their omission should have been detected by Python as a code error.

Stephen



--
Community Discussion Forum for Anaconda
---
You received this message because you are subscribed to the Google Groups "Anaconda - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to anaconda+u...@anaconda.com.
To view this discussion on the web visit https://groups.google.com/a/anaconda.com/d/msgid/anaconda/DBAE2074-CC5D-42F8-9E46-5390E2D009CA%40cs.uoregon.edu.

Toby, Brian H.

unread,
Sep 14, 2021, 4:42:25 PM9/14/21
to anac...@anaconda.com, Stephen Derenzo
No, it should not be a code error. Python allows you to write code where variables point to routines. 

Silly example, but I do use this type of coding:

if keepOpen:
    useme = outfile.flush
else:
    useme = outfile.close
useme()

It is often useful to pass a function or method into a routine so it can be called where needed. 

Brian

David Wilkins

unread,
Sep 14, 2021, 7:23:41 PM9/14/21
to anac...@anaconda.com
x = outfile.close # Assigns the function close to x
outfile.close # Usually an error, but returns the close function (does NOT call the function)

# When is anything like this useful?

debug = print

debug(x, y, z) # prints values of x, y and z but also makes clear this is just for debugging not a “real” print statement we need to keep.

# Another case : a function may have a long name collaborate_and_materialize( )
snappy = collaborate_and_materialize # Now you can call it using snappy

# Ideally, python should issue a gentle warning for outfile.close # Did you mean to call the close function? if so, do outfile.close()

Dave
> To view this discussion on the web visit https://groups.google.com/a/anaconda.com/d/msgid/anaconda/CAAmTvGWJBPz-BVBgpGpEdiHOkhoj63J_cDwcU1nw1K1qC2%3Doyw%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages