每当我尝试在Python 2.7中覆盖文件时,会发生这种情况,代码:
a = open('hello.txt')
a.write('my name is mark')
我的错误是:
Traceback (most recent call last):
File "C:\Users\Mark Malkin\Desktop\New folder\opener.py", line 2, in <module>
a.write('my name is mark')
IOError: File not open for writing
答案 0 :(得分:2)
来自open
上的文档:
如果省略mode,则默认为'r'。
改为使用,
a = open('hello.txt', 'w')
或者更好,
with open('hello.txt', 'w') as f:
f.write('my name is mark')