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 189009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:08:12+00:00 2026-05-11T16:08:12+00:00

I hope I framed the question right. I am trying to force myself to

  • 0

I hope I framed the question right. I am trying to force myself to be a better programmer. By better I mean efficient. I want to write a program to identify the files in a directory and read each file for further processing. After some shuffling I got to this:

for file in os.listdir(dir):
    y=open(dir+'\\'+file,'r').readlines()
    for line in y:
        pass
    y.close()

It should be no surprise that I get an AttributeError since y is a list. I didn’t think about that when I wrote the snippet.

I am thinking about this and am afraid that I have five open files (there are five files in the directory specified by dir.

I can fix the code so it runs and I explicitly close the files after opening them. I am curious if I need to or if Python handles closing the file in the next iteration of the loop. If so then I only need to write:

for file in os.listdir(dir):
    y=open(dir+'\\'+file,'r').readlines()
    for line in y:
        pass

I am guessing that it(python) does handle this effortlessly. The reason I think that this might be handled is that I have changed the object/thing that y is referencing. When I start the second iteration there are no more memory references to the file that was opened and read using the readlines method.

  • 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-05-11T16:08:12+00:00Added an answer on May 11, 2026 at 4:08 pm

    Python will close open files when they get garbage-collected, so generally you can forget about it — particularly when reading.

    That said, if you want to close explicitely, you could do this:

    for file in os.listdir(dir):
        f = open(dir+'\\'+file,'r')
        y = f.readlines()
        for line in y:
            pass
        f.close()
    

    However, we can immediately improve this, because in python you can iterate over file-like objects directly:

    for file in os.listdir(dir):
        y = open(dir+'\\'+file,'r')
        for line in y:
            pass
        y.close()
    

    Finally, in recent python, there is the ‘with‘ statement:

    for file in os.listdir(dir):
        with open(dir+'\\'+file,'r') as y:
            for line in y:
                pass
    

    When the with block ends, python will close the file for you and clean it up.

    (you also might want to look into os.path for more pythonic tools for manipulating file names and directories)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope you all are fine and rocking your code editors. My question is how
Hope this question is not stupid since I am an amateur web designer. I
Hope you're having a good Friday and stuff... okay, so here's my question: All
This is kind of an odd question, I hope this enough information to go
I'm new to Quartz2d so I hope this is an easy question to answer.
I hope someone can direct me on the right path before I put a
I have a question regarding Rotation (R) and Translation (T) matrices, I hope someone
Thanks for taking a look at my question :) I'm making a program to
I hope you won't find my question too silly, i did a lot of
Hope my question does not pose as too wide. So I try to frame

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.