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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:19:25+00:00 2026-05-29T05:19:25+00:00

After reading some posts, it seems like you can open a file for both

  • 0

After reading some posts, it seems like you can open a file for both reading and writing with the mode of ‘r+’ or ‘w+’. However, trying to use these modes always give me weird results:

  1. If I use ‘r+’, call file.read(), and then call file.write(‘str’),
    there’ll be an error of “IOError: [Errno 0] Error”
  2. If I use ‘r+’, call file.write(‘str’), and then call file.read(),
    it’ll return unexpected and very long content(looks like the inside
    of some object)
  3. If I use ‘w+’, calling file.read() will return empty string

What I’m trying to do is open a file, read the content, modify it, and write back. Currently I’m opening it with ‘r’, change the content, and open it again with ‘w’ and write back. Is this a good way of doing it?

There’s an example at http://snipt.org/zglJ0

I’m using window 7 and python 2.7.2

  • 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-29T05:19:25+00:00Added an answer on May 29, 2026 at 5:19 am

    You have to flush() when switching between reading and writing a file that’s been opened in an update mode. Or I think you can also seek(). This is caused by some weird behavior in the Windows file implementation in Python 2.x; they fixed it in 3.x.

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

Sidebar

Related Questions

After reading some posts like this one: Choose File Dialog It appears that Android
I've always used a *.h file for my class definitions, but after reading some
I've trying to get some vertical text on a little test webpage after reading
After reading some posts here on the advantages of using source control for a
I'm looking for a HTML and JavaScript parser in .NET. After reading some posts
after reading some posts about implementing shaking on 3.0, I think I get the
After reading some threads on misuses of exceptions (basically saying you don't want to
After reading some material on this subject I'm still not sure what the difference
I came across couple of questions about OCL expressions. After reading some university slides
After some reading and R&D, I have came to conclusion that ? Forms in

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.