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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:12:06+00:00 2026-06-17T08:12:06+00:00

I am in python and there is a lot of ways to access files.

  • 0

I am in python and there is a lot of ways to access files.

Method 1:

fp = open("hello.txt", "w")
fp.write("No no no");
fp.close()

fp = open("hello.txt", "r")
print fp.read()
fp.close()

Method 2:

open("hello.txt", "w").write("hello world!")
print open("hello.txt", "r").read()

Method 3:

with open("hello.txt","w") as f:
    f.write("Yes yes yes")

with open("hello.txt") as f:
    print f.read()

Is there a specific advantage in using each of these?

Stuff I know:

  • Method 2 and Method 3 closes the file automatically, but
    Method 1 doesn’t.
  • Method 2 doesn’t give you a handle to do multiple operations.
  • 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-17T08:12:07+00:00Added an answer on June 17, 2026 at 8:12 am

    You should use the third method.

    There is a common pattern in programming where to use some object you have set it up, run your code, and tear it down again. File handles are one example of this: you have to open the file, run your code, and then close the file. This last is not optional — it’s important for the operating system to know that you are done with it, and for Python to flush all the data out of its IO buffers.

    Now, CPython is a reference counted language. That means that it counts how many pieces of code ‘know about’ a given object, so that when that count becomes zero it can clean up said object and reuse its space in memory. In method 2, the reference count of the file object becomes zero, which allows Python to clean it up. And file objects’ cleanup method also closes them. However, you should in general not rely on this — reference counting is an implementation detail of the standard version of Python, and there’s no guarantee that whatever you’re using to run the program will do the same. That’s why you shouldn’t use method 2.

    Method 1 is better, because you explicitly close the file — as long as you reach the .close() function call! If an exception was thrown in the middle of that code block, the close would not be reached, and the file would not be explicitly closed. So you should really wrap the middle code in a try... finally block.

    Method 3 is therefore best: you use the with statement — an idiomatic way of enclosing the .close in a finally block — to close the file, so you don’t have to worry about the extra syntactic fluff of try... except.

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

Sidebar

Related Questions

There are a lot of ways in python provided by the standard installation to
There is a lot of discussions of Python vs Ruby, and I all find
Am trying to write a piece of python code that calculate and print 1000
There are a lot of articles around the web concerning Python performance. The first
In Python there is a maximum recursion depth. Seems it is because Python is
in languages like PHP or Python there are convenient functions to turn an input
I'm looking for presence of an element in a list. In Python there is
Are there Python 3 bindings for Clutter? If so, how can I get them
Is there a Python library that can parse an CSS selector and emit an
Are there any Python JSON parsers that will cope with trailing commas? (I'm consuming

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.