Skip to content

open() and file.write() without file.close() #82729

Closed

Description

@kryptomatrix
BPO 38548
Nosy @vstinner
Superseder
  • bpo-17852: Built-in module _io can lose data from buffered files in reference cycles
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-10-21.12:20:44.094>
    created_at = <Date 2019-10-21.12:12:20.903>
    labels = ['type-feature', '3.7', 'docs']
    title = 'open() and file.write() without file.close()'
    updated_at = <Date 2019-10-21.12:20:44.092>
    user = 'https://bugs.python.org/kryptomatrix'

    bugs.python.org fields:

    activity = <Date 2019-10-21.12:20:44.092>
    actor = 'vstinner'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-10-21.12:20:44.094>
    closer = 'vstinner'
    components = ['Documentation']
    creation = <Date 2019-10-21.12:12:20.903>
    creator = 'kryptomatrix'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38548
    keywords = []
    message_count = 2.0
    messages = ['355062', '355063']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'docs@python', 'kryptomatrix']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '17852'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue38548'
    versions = ['Python 3.7']

    Activity

    1. kryptomatrix commented on Oct 21, 2019

      kryptomatrixmannequin
      MannequinAuthor

      Consider the following program:

      f = open("out.txt", "w")
      f.write("abc\n")
      exit(0)

      Please note the absence of f.close().
      The documentation
      https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
      says that you should use f.close() or with f = open(), but is not clear whether the program above without f.close() is guaranteed to write. The tutorial says:
      "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."
      For me this sounds like even without f.close() the file is guaranteed to be written. If it is not guaranteed to be written, you should fix the documentation, if it is guaranteed to be written, then I will open another issue because the following program does not write into out.txt on my machine:

      from sympy.core import AtomicExpr
      class MyWeirdClass(AtomicExpr):
      	def __init__(self):
      		pass
      f = open("out.txt", "w")
      f.write("abc\n")
      exit(0)

      Note: sys.version is: "3.7.3 (default, Oct 7 2019, 12:56:13) \n[GCC 8.3.0]"

    2. added
      docs
      type-feature
      on Oct 21, 2019
    3. vstinner commented on Oct 21, 2019

      @vstinner
      Member

      The issue is a duplicate of bpo-17852.

    4. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)docstype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions