总结起来就是:得以“w”的方式写入文件
py2.7
options = [1,2,3,4,5,6,7,8,9,10]
print(options)
print("write as str")
with open("str.txt",'w') as f:
pickle.dump(options,f)
f.close()
print("write as wb")
with open("wb.txt",'wb') as f:
pickle.dump(options,f)
f.close()
接下来看看两个文件的区别
$ md5sum str.txt wb.txt
fd57223c5c67abda1dba90e06bf9acfb str.txt
fd57223c5c67abda1dba90e06bf9acfb wb.txt
py3
执行同样的文件, 得到了以下的结果
write as str
Traceback (most recent call last):
File "tmp.py", line 61, in
pickle.dump(options,f)
TypeError: write() argument must be str, not bytes
看起来py3对文件类型进行了检查,及时抛出了错误