2013-01-23 37 views
2

我試圖上傳文本文件中使用此代碼的Dropbox:的Dropbox的Python API:文件大小檢測可能失敗

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:\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) 

谷歌給了我這個沒有幫助。

回答

8

聽起來像你是新行統一的受害者。該文件對象報告18個字節("abcdefghijklmnop\r\n")的文件大小,但只讀17個字節("abcdefghijklmnop\n")。

以二進制方式打開文件,以避免這種情況:

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

The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading.

+0

非常感謝你,我決不會想通了這一點。 – Rabbit