2013-08-26 17 views
0

覆蓋文件每當我嘗試覆蓋文件在Python 2.7,這是發生了什麼,代碼:不能在Windows

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 

回答

2

從上open文檔:

如果省略模式,則默認爲'r'。

爲了編寫而使用,

a = open('hello.txt', 'w') 

或者更好的是,

with open('hello.txt', 'w') as f: 
    f.write('my name is mark') 
+0

只要我回家給一個Python IDLE我會嘗試。 – user164814

+0

工作後,我做到了這一點:a = open('hello.txt','w')a.write(「bla bla」)a.close() – user164814

+0

感謝您的幫助+1 – user164814