AttributeError: 'tuple' object has no attr

2019-09-19 01:26发布

问题:

I have got this code's lines:

import dropbox

#some lines for initialize API's

download = self.client.get_file_and_metadata(selected_path)
current_working = os.getcwd()
out = open(current_working+self.filename,'w')
out.write(download.read())
out.close()

where 'selected_path' is the path where there is the file I want to download and 'current_working' is the path where I want to save the file. When I run the script, I retrieve this error:

AttributeError: 'tuple' object has no attribute 'read'

The file that I want to download is a GPG crypted file, but I don't think that is this the matter. Sorry for my bad english.

回答1:

Function get_file_and_metadata returns tuple: file and metadata.

from here: https://www.dropbox.com/developers/core/start/python

In addition to the file, the method also returns the file's metadata at its current revision. Every time a change is made to the file, the rev field of the file's metadata changes as well. By saving the revision when you download the file, you'll be able to tell if that file has been updated by another computer or device and choose to download the newer revision of that file.

change your call to self.client.get_file_and_metadata like this:

download, metadata = self.client.get_file_and_metadata(selected_path)

or just use get_file if you dont need the metadata:

download = self.client.get_file(selected_path)