Mik*_*oli 3 python dropbox-api
我正在尝试遍历python中的文件夹结构,并将找到的每个文件上传到指定的文件夹.问题是它上传的文件名正确,但没有内容,文件大小只有10个字节.
import dropbox, sys, os
try:
dbx = dropbox.Dropbox('some_access_token')
user = dbx.users_get_current_account()
except:
print ("Negative, Ghostrider")
sys.exit()
rootdir = os.getcwd()
print ("Attempting to upload...")
for subdir, dirs, files in os.walk(rootdir):
for file in files:
try:
dbx.files_upload("afolder",'/bfolder/' + file, mute=True)
print("Uploaded " + file)
except:
print("Failed to upload " + file)
print("Finished upload.")
Run Code Online (Sandbox Code Playgroud)
您的来电dbx.files_upload("afolder",'/bfolder/' + file, mute=True)说:"发送文本afolder并将其写为名为'/bfolder/' + file" 的文件.
files_upload(f,path,mode = WriteMode('add',None),autorename = False,client_modified = None,mute = False)
使用请求中提供的内容创建一个新文件.参数:
- f - 类似字符串或文件的obj数据.
- path(str) - 用户Dropbox中保存文件的路径.
....
意思是f必须是文件的内容(而不是文件名字符串).
import dropbox, sys, os
dbx = dropbox.Dropbox('token')
rootdir = '/tmp/test'
print ("Attempting to upload...")
# walk return first the current folder that it walk, then tuples of dirs and files not "subdir, dirs, files"
for dir, dirs, files in os.walk(rootdir):
for file in files:
try:
file_path = os.path.join(dir, file)
dest_path = os.path.join('/test', file)
print 'Uploading %s to %s' % (file_path, dest_path)
with open(file_path) as f:
dbx.files_upload(f, dest_path, mute=True)
except Exception as err:
print("Failed to upload %s\n%s" % (file, err))
print("Finished upload.")
Run Code Online (Sandbox Code Playgroud)
编辑:对于Python3,应使用以下内容:
dbx.files_upload(f.read(), dest_path, mute=True)