我得到屬性錯誤:'int'對象沒有屬性'write'。Python os.write(filehandler,data):TypeError一個整數要求
這裏是我的腳本
data = urllib.urlopen(swfurl)
save = raw_input("Type filename for saving. 'D' for same filename")
if save.lower() == "d":
# here gives me Attribute Error
fh = os.open(swfname,os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
fh.write(data)
# #####################################################
這裏的一部分是錯誤:
Traceback (most recent call last):
File "download.py", line 41, in <module>
fh.write(data)
AttributeError: 'int' object has no attribute 'write'
在這種情況下,內置的open()是不恰當的因爲它缺少傳遞標誌O_WRONLY,O_CREAT和O_TRUNC所需的低級API。海報是正確的使用os.open()。如果你不熟悉這種機制,請參閱@ oleg的答案,該答案概述了內建的open()和os.open(),並給出了何時使用os.open()的一些指導。 –