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 Answer 1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.