I am trying to implement a dropbox app which downloads files from user's Dropbox account.While creating destination path in user's local directory,it crashes saying
Error occured [400] {u'path : u''invalid path /New folder\\img1.jpg : character at index 11 backslashes not allowed}
I thought the dropbox's folder hierarchy uses forward slashes to represent nesting of dorectories, and windows uses backward slashes, so they might be conflicting. Then I used python's BIF replace() as follows for different paths
sample_path.replace( "\\", "/" )
but still
complete_path
variable in my code is giving path containing backslash,after which the program crashes. the folder hierarchy in my dropbox account is :
New Folder :
Img1.jpg
dtu.jpg
img.jpg
the code is :
def download_file(self,source_path,target_path):
print 'Downloading %s' % source_path
file_path = os.path.expanduser(target_path)
(dir_path,tail) = os.path.split(target_path)
self.check_dir(dir_path)
to_file = open(file_path,"wb")
print source_path+"!!!!!!!!!!!!!!!!!!!!!!!!!!"
source_path.replace("\\","/")
f= self.mClient.get_file(source_path) # request to server !
to_file.write(f.read())
return
def download_folder(self, folderPath):
# try to download 5 times to handle http 5xx errors from dropbox
try:
response = self.mClient.metadata(folderPath)
# also ensure that response includes content
if 'contents' in response:
for f in response['contents']:
name = os.path.basename(f['path'])
complete_path = os.path.join(folderPath, name)
if f['is_dir']:
# do recursion to also download this folder
self.download_folder(complete_path)
else:
# download the file
self.download_file(complete_path, os.path.join(self._target_folder, complete_path))
else:
raise ValueError
except (rest.ErrorResponse, rest.RESTSocketError, ValueError) as error:
print 'An error occured while listing a directory. Will try again in some seconds.'
print "Error occured "+ str(error)