当我运行以下代码时:
import shelve
input = open("input.txt",)
shelveFile = shelve.open("myData")
shelveFile["inputFile"] = input
input.close()
shelveFile.close()
我希望搁置文件myData
保存文件对象input
。相反,运行代码会产生以下错误:
Traceback (most recent call last):
File "/Users/ashutoshmishra/Documents/Sandbox/Sandbox3.py", line 5, in <module>
shelveFile["inputFile"] = input
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shelve.py", line 124, in __setitem__
p.dump(value)
TypeError: cannot serialize '_io.TextIOWrapper' object
我想知道为什么我无法将文件对象input
保存到搁置文件myData
?
答案 0 :(得分:0)
以下答案取自@DanD。上述评论。
阅读文件:
shelveFile["inputFile"] = input.read()