我正在尝试使用以下代码将文本文件上传到Dropbox:
def uploadFile(file):
f = open('logs/%s.txt' % file)
response = client.put_file('/%s.txt' % file, f)
print "Uploaded log file %s" % file
连接到 Dropbox 工作正常,只是当我上传文件时收到此错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:Python27libsite-packagesdropbox_python_sdk-1.5.1-py2.7.eggdropbox
client.py", line 352, in put_file
return self.rest_client.PUT(url, file_obj, headers)
File "C:Python27libsite-packagesdropbox_python_sdk-1.5.1-py2.7.eggdropbox
rest.py", line 265, in PUT
return cls.IMPL.PUT(*n, **kw)
File "C:Python27libsite-packagesdropbox_python_sdk-1.5.1-py2.7.eggdropbox
rest.py", line 211, in PUT
return self.request("PUT", url, body=body, headers=headers, raw_response=raw
_response)
File "C:Python27libsite-packagesdropbox_python_sdk-1.5.1-py2.7.eggdropbox
rest.py", line 174, in request
raise util.AnalyzeFileObjBug(clen, bytes_read)
dropbox.util.AnalyzeFileObjBug:
Expected file object to have 18 bytes, instead we read 17 bytes.
File size detection may have failed (see dropbox.util.AnalyzeFileObj)
谷歌没有给我任何帮助。
听起来你是换行符统一的受害者。文件对象报告的文件大小为 18 字节 ( "abcdefghijklmnoprn"
),但您只读取了 17 个字节 ("abcdefghijklmnopn"
)。
以二进制模式打开文件以避免这种情况:
f = open('logs/%s.txt' % file, 'rb')
默认设置是使用文本模式,这可能会在写入和读取时将
'n'
字符转换为特定于平台的表示形式。