Python Forum
Does python guarantees that file.write() works without file.close() ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Does python guarantees that file.write() works without file.close() ?
#4
(Oct-18-2019, 01:39 PM)perfringo Wrote:
(Oct-18-2019, 01:20 PM)kryptomatrix Wrote: Is it guaranteed, that "out.txt" will contain "abc"

Only taxes and death are guaranteed, everything else is optional Smile.

In more serious note - this 'guarantee' can't be given as there might be another processes which can overwrite the file. Yes, 'abc' will be written into file but this is not the same that 'guaranteed to contain' in some point after the writing.
I'm assuming that no other process messes with it. The question is whether this script is guaranteed to write, because the following script does not write into the file:
from sympy.core import AtomicExpr

class MyWeirdClass(AtomicExpr):
	def __init__(self):
		pass

f = open("out.txt", "w")
f.write("abc\n")
I am looking for a point in the official specification that says whether this is guaranteed to write or not. Because if it is guaranteed to write, this is a bug in python.

(Oct-18-2019, 01:31 PM)buran Wrote: use with context manager
with open("out.txt", "w") as f:
    f.write("abc\n")
it will close it for you

from the docs:
Quote:It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with is also much shorter than writing equivalent try-finally blocks:
>>>

>>> with open('workfile') as f:
...     read_data = f.read()

>>> # We can check that the file has been automatically closed.
>>> f.closed
True
If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.
I know the with keyword, but that wasn't the question. The question was if the standard guarantees that the my weird code works.
Reply


Messages In This Thread
RE: Does python guarantees that file.write() works without file.close() ? - by kryptomatrix - Oct-18-2019, 10:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What does .flush do? How can I change this to write to the file? Pedroski55 3 292 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Python openyxl not updating Excel file MrBean12 1 387 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 450 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Last record in file doesn't write to newline gonksoup 3 485 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  connect sql by python using txt. file dawid294 2 486 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 1,257 Dec-14-2023, 08:03 AM
Last Post: shanoger
  write to csv file problem jacksfrustration 11 1,633 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,568 Nov-09-2023, 10:56 AM
Last Post: mg24
  Replace a text/word in docx file using Python Devan 4 3,680 Oct-17-2023, 06:03 PM
Last Post: Devan
  Help creating shell scrip for python file marciokoko 10 1,448 Sep-16-2023, 09:46 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020