python open内置函数:模式“aw”和“a”或“aw”和“w”之间的区别

时间:2017-03-16 02:56:32

标签: python built-in

当“a”和“w”模式同时出现时,python如何处理这种情况,即

f = open(filename, "wa")

f = open(filename, "aw")

我注意到这里有一个类似的问题python open built-in function: difference between modes a, a+, w, w+, and r+?,但它并没有解释我的问题。 我也尝试在这里阅读C中的源代码https://hg.python.org/cpython/file/2.7/Objects/fileobject.c,但我的问题也没有解释。

我已经尝试了上面的代码,并且当给出“a”和“w”时,似乎Python只采用第一个参数。这没有意义,为什么Python不会引发错误呢?

1 个答案:

答案 0 :(得分:1)

这已在python 3中修复。

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

回溯:

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