Python:使用Dropbox API - 保存.ODT文件
问题描述:
我正在使用Dropbox API和Python。我没有使用Dropbox API的问题,我可以毫无问题地完成所有认证步骤。Python:使用Dropbox API - 保存.ODT文件
当我使用此代码:
pdf_dropbox = client.get_file('/Example.pdf')
new_file = open('/home/test.pdf','w')
new_file.write(pdf_dropbox.read())
我在路径/home/test.pdf
生成一个文件,这是一个PDF文件,内容显示一样的原件。
但是当我尝试用.odt
文件相同的代码,它无法生成新的文件:
odt_dropbox = client.get_file('/Example.odt')
new_file = open('/home/test_odt.odt','w')
new_file.write(odt_dropbox.read())
这个新文件test_odt.odt有错误,我无法看到它的内容。
# With this instruction I have the content of the odt file inside odt_dropbox
odt_dropbox = client.get_file('/Example.odt')
- 至极是保存一个
odt
文件的内容的最好方式? - 有没有更好的方法来写LibreOffice文件?
我会很感激的任何有益的信息,
感谢
答
解决了,我忘了两两件事:
-
公开赛二进制写作
文件wb
代替w
new_file = open('/home/test_odt.odt','wb')
关闭创建后的文件:
new_file.close()
,使冲洗
全码:
odt_dropbox = client.get_file('/Example.odt')
new_file = open('/home/test_odt.odt','wb')
new_file.write(odt_dropbox.read())
new_file.close()
我认为你的意思'close'不'save'在你的代码在这里。另外,您可能想通过'with'使用上下文。这将只是'open('/ home/test_odt.odt','wb')as new_file:new_file.write(odt_dropbox.read())' – smarx 2014-09-04 15:25:46
Thanks!我写错了,因为你说'.save()'而不是'.close()'。现在它已经修复了。谢谢! – AlvaroAV 2014-09-05 10:22:32