Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8071839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:49:22+00:00 2026-06-05T13:49:22+00:00

For example I have the following code: d = [l for l in open(‘a.txt’,

  • 0

For example I have the following code:

d = [l for l in open('a.txt', 'r')]

After d is created, will the stream opened in the list comprehension get closed automatically ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:49:23+00:00Added an answer on June 5, 2026 at 1:49 pm

    “Maybe”.

    In cPython, which uses refcounting, the file will be closed as soon as the list comprehension finishes (and all references to the file object are lost).

    But the Python standard does not require that it be closed. For example, the file will not be closed immediately in jython, which uses the JVM garbage collector.

    The “preferred” method of ensuring that resources are correctly closed is the with statement:

    with open(…) as f:
        d = [l for l in f]
    

    This will guarantee that the file get closed.

    And, as @astynax points out, you might be able to use d = f.readlines() here, as it would have the same semantics as the list comprehension.

    To prove this to yourself (in cpython):

    >>> class Foo(object):
    ...     def __del__(self):
    ...         print "in __del__"
    ...     def __iter__(self):
    ...         return iter([1, 2, 3, 4])
    ...
    >>> [ x for x in Foo() ]
    in __del__
    [1, 2, 3, 4]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML code: <div onclick=window.open('http://example.com') > <p>1234 Main St, New York,
I have the following example code <?php `echo test > /tmp/test.txt`; $f=fopen(/tmp/test.txt,w+); `rm /tmp/test.txt`;
Say I have the following code: <div onclick='location.href=http://www.example.com/'> <a href='#' onclick='alert(blah)'>click</a> </div> Is there
On a report I have the following code for a field: =Sum([PartQty]*[ModuleQty]) Example results
Say suppose I have the following Java code. public class Example { public static
I have the following code to hide an essay question or open text. I'd
I have the following code which I call google #!/bin/bash q=$1 open http://www.google.com/search?q=$q It
I have the following code, which will not work. The javascript gives no errors
I'm trying to figure out how XSLT process namespace prefixes and have following example:
I have following structure with example data: id season_id title 1 1 Intro 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.