我想實現一個Dropbox的應用程序,這將下載從用戶的Dropbox的account.While建立在用戶的本地目錄目標路徑的文件,它崩潰說無效的路徑,反斜槓不允許
發生錯誤[400]【U 「路徑:u''invalid路徑/新建文件夾\\ img1.jpg:字符在指數11反斜槓不允許}
我認爲Dropbox的文件夾層次使用正斜槓表示dorectories的嵌套和Windows使用落後斜線,所以它們可能會相互衝突。然後我用Python的BIF替換()對於不同的路徑如下
sample_path.replace( 「\\」, 「/」)
但仍
complete_path
我的代碼中的變量是給出包含反斜槓的路徑,之後程序崩潰。 在我的Dropbox帳戶的文件夾層次是:
New Folder :
Img1.jpg
dtu.jpg
img.jpg
代碼:
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)
你怎麼調用這些函數?你在Windows上運行? –
函數調用鏈接爲 - main() - > init_download() - > download_folder(),是啊我正在運行windows bash – vertexion
順便說一句,把替換無處不在幫助。使用路徑模塊將無需替換任何東西。僅在發送到服務器之前替換路徑。 –