1

How does python deal with the circumstance when mode "a" and "w" appear at the same time, i.e.

f = open(filename, "wa")

or

f = open(filename, "aw")

I noticed there was a similar question here, python open built-in function: difference between modes a, a+, w, w+, and r+?, but it doesn't explain my question. I also tried to read the source code in C here https://hg.python.org/cpython/file/2.7/Objects/fileobject.c, but my question was not explained, either.

I have tried the above codes, and it seems that Python would take only the first parameter when both "a" and "w" are given. This makes no sense, why Python doesn't raise an error instead?

1 Answers1

1

This has been fixed in python 3.

with open("somefile.txt", "wa") as f:
    ...

Traceback:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    with open("somefile.txt", "wa") as f:
ValueError: must have exactly one of create/read/write/append mode
Jessie
  • 2,319
  • 1
  • 17
  • 32
  • Yes, thank you for the answer. I forgot to mention I was using Python 2.7. Just one more question, in Python 2.7, is my speculation of only the first parameter being taking when both "a" and "w" are given correct? – Tatsumi Sanzenin Mar 16 '17 at 03:34