2

我正在尝试使用以下代码将文本文件上传到 Dropbox:

def uploadFile(file):
    f = open('logs/%s.txt' % file)
    response = client.put_file('/%s.txt' % file, f)
    print "Uploaded log file %s" % file

连接到保管箱工作得非常好,只是在我上传文件时收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\dropbox_python_sdk-1.5.1-py2.7.egg\dropbox
\client.py", line 352, in put_file
    return self.rest_client.PUT(url, file_obj, headers)
  File "C:\Python27\lib\site-packages\dropbox_python_sdk-1.5.1-py2.7.egg\dropbox
\rest.py", line 265, in PUT
    return cls.IMPL.PUT(*n, **kw)
  File "C:\Python27\lib\site-packages\dropbox_python_sdk-1.5.1-py2.7.egg\dropbox
\rest.py", line 211, in PUT
    return self.request("PUT", url, body=body, headers=headers, raw_response=raw
_response)
  File "C:\Python27\lib\site-packages\dropbox_python_sdk-1.5.1-py2.7.egg\dropbox
\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)

谷歌没有给我任何帮助。

4

1 回答 1

8

听起来您是换行符统一的受害者。文件对象报告文件大小为 18 字节 ( "abcdefghijklmnop\r\n"),但您只读取了 17 字节 ( "abcdefghijklmnop\n")。

以二进制模式打开文件以避免这种情况:

f = open('logs/%s.txt' % file, 'rb')

默认是使用文本模式,它可以'\n'在写入和读取时将字符转换为特定于平台的表示。

于 2013-01-23T19:14:26.657 回答