即时通讯使用anaconda(python2.7)和即时通讯进行程序的程序化,并收到错误。我的程序将文件写入文件的地方,出现属性错误。有人可以帮我修复我的代码吗?此外,任何使它运行更快的技巧(cythonization等)也将不胜感激!

到目前为止,这是我的代码:

http://pastebin.com/gmC7rtBB

这是错误代码:

Traceback (most recent call last):

  File "<ipython-input-1-e95db2234cf8>", line 1, in <module>
    runfile('C:/Users/Jay/Desktop/quantum emulator/qemulatortestt.py', wdir='C:/Users/Jay/Desktop/quantum emulator')

  File "C:\Users\Jay\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 585, in runfile
    execfile(filename, namespace)

  File "C:/Users/Jay/Desktop/quantum emulator/qemulatortestt.py", line 246, in <module>
    rewrite(task, asciivalue)

  File "C:/Users/Jay/Desktop/quantum emulator/qemulatortestt.py", line 77, in rewrite
    fle.write(cnt)

AttributeError: 'str' object has no attribute 'write'

最佳答案

with open(fle, 'r+') as f:
    f.write(cnt)
    f.close()
    return f


fle是表示文件路径的字符串。

关于python - 为什么会出现此错误,我该如何解决?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24645686/

10-11 06:02