簡體   English   中英

Dropbox通過python錯誤上傳文件

[英]Dropbox upload file by python error

我正在嘗試將文件列表上傳到保管箱,但出現各種錯誤。 我嘗試了這個,然后嘗試了網上的所有內容。 但是我仍然無法正常工作。

# Include the Dropbox SDK libraries
from dropbox import rest, session
import webbrowser
import os
import glob
import zipfile
import datetime
from dropbox import client

# Get your app key and secret from the Dropbox developer website

# (app keys defined here)

# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
ACCESS_TYPE = 'app_folder'
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)

request_token = sess.obtain_request_token()

url = sess.build_authorize_url(request_token)

file_list = []

#get the date
now = datetime.datetime.now()

def initialize():

    #print "url:", url
    # open a public URL, in this case, the webbrowser docs
    #url = "http://docs.python.org/library/webbrowser.html"
    webbrowser.open(url)
    print "Please click the 'Allow' button to Authorize..."
    print """
    Please select by entering the specific number...
    1 Backup all my files by one by one
    2 Backup specific folder
    3 Backup specific file
    4 Get my account details
    5 About this software
    6 Exit
    """
    try:
        # This will fail if the user didn't visit the above URL and hit 'Allow'
        access_token = sess.obtain_access_token(request_token)
    except:
        print "Error has occured"

def getAccountInfo():
    from dropbox import client
    client = client.DropboxClient(sess)
    account_info_dict = client.account_info()
    print "linked account:"

    for item in account_info_dict:
        if type(account_info_dict[item]) == dict:
           inner_dict = account_info_dict[item]
           for item1 in inner_dict:
               print item1, ":", inner_dict[item1]
        print item, ":", account_info_dict[item]

def getAllFiles():
   for dirname, dirnames, filenames in os.walk('I:/'):
       for subdirname in dirnames:
           print os.path.join(dirname, subdirname)
       for filename in filenames:
           file_name = os.path.join(dirname, filename)
           print file_name
           file_list.append(file_name)
   return file_list

def upload_one_by_one(sess):
   from dropbox import client
   files = getAllFiles()
   client = client.DropboxClient(sess)
   #zip_file_name = now.strftime("%Y%m%d%H%M")+ ".zip"    
   #z = zipfile.ZipFile(zip_file_name, "w")
   for file_item in files:
       #z.write(file_item)
       #f = open(zip_file_name)
       response = client.put_file("test/", file_item)
       print "uploaded:", response
       break


initialize()
#getAccountInfo()
upload_one_by_one(sess)

client.put_file()參數存在問題。 我正在嘗試將圖像文件上傳到測試文件夾中。 但是,它以文本文件的形式上傳,其中包含圖像文件的路徑。

我不能通過Dropbox SDK將圖像文件上傳到Dropbox嗎?

似乎您沒有打開要上傳的文件,這可能有效:

for file_item in files:
  file = open(file_item)
  response = client.put_file("test/", file)

put_file()可以使用類似於文件的對象或代表文件內容的字符串。 如果您輸入文件名,它將不會自動讀取文件內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM