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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:33:33+00:00 2026-05-14T18:33:33+00:00

I need to open a file for reading and writing. If the file is

  • 0

I need to open a file for reading and writing. If the file is not found, it should be created. It should also be treated as a binary for Windows. Can you tell me the file mode sequence I need to use for this?

I tried ‘r+ab’ but that doesn’t create the files if they are not found.

Thanks

  • 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-14T18:33:34+00:00Added an answer on May 14, 2026 at 6:33 pm

    The mode is ab+ the r is implied and ‘a’ppend and (‘w’rite ‘+’ ‘r’ead) are redundant. Since the CPython (i.e. regular python) file is based on the C stdio FILE type, here are the relevant lines from the fopen(3) man page:

    • w+ Open for reading and writing.
      The file is created if it does not
      exist, otherwise it is truncated.
      The stream is positioned at the
      beginning of the file.

    • a+ Open for reading and appending (writing at end of file).
      The file is created if it does not
      exist. The initial file position
      for reading is at the beginning of
      the file, but output is always
      appended to the end of the file.

    With the “b” tacked on to make DOS happy. Presumably you want to do something like this:

    >>> f = open('junk', 'ab+')
    >>> f
    <open file 'junk', mode 'ab+' at 0xb77e6288>
    >>> f.write('hello\n')
    >>> f.seek(0, os.SEEK_SET)
    >>> f.readline()
    'hello\n'
    >>> f.write('there\n')
    >>> f.seek(0, os.SEEK_SET)
    >>> f.readline()
    'hello\n'
    >>> f.readline()
    'there\n'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey can any one tell me should i need open a port for outgoing
I need to open a log file for writing. Trouble is, many things may
Alright so i need to open a .txt file that will be created in
When I open a file using fopen($filename, 'w+') (that is, reading and writing), it
I need an Open Dialog File Chooser that opens files for reading only. I
I want to open a file for reading, the C++ way. I need to
In my application i need to open pdf file.For that i have prepared code
I have a xml file which I need to open with Microsoft Word 2007.
I have a huge file and need to read it and process. with open(source_filename)
I need the browser to cache a large, mostly static .php file. I open

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.