无法将字符串值更改为具有字符串值的变量
Cant change string value to variable with string value
我将文件上传到保管箱 api,但它 post 在保管箱中从根文件夹开始我计算机上的所有目录。我的意思是你的项目文件夹在文件夹 home 中,而不是用户,直到你转到文件 sours 文件夹。如果我切割那个结构库就看不到它是文件,而不是字符串并给出错误信息。
我的代码是:
def upload_file(project_id, filename, dropbox_token):
dbx = dropbox.Dropbox(dropbox_token)
file_path = os.path.abspath(filename)
with open(filename, "rb") as f:
dbx.files_upload(f.read(), file_path, mute=True)
link = dbx.files_get_temporary_link(path=file_path).link
return link
有效,但我需要类似的东西:
file_path = os.path.abspath(filename)
chunks = file_path.split("/")
name, dir = chunks[-1], chunks[-2]
这让我犯了这样的错误:
dropbox.exceptions.ApiError: ApiError('433249b1617c031b29c3a7f4f3bf3847', GetTemporaryLinkError('path', LookupError('not_found', None)))
如何在路径中只设置父文件夹和文件名?
例如,如果我有
/home/user/project/file.txt
我需要
/project/file.txt
我假设以下代码应该有效:
def upload_file(project_id, filename, dropbox_token):
dbx = dropbox.Dropbox(dropbox_token)
abs_path = os.path.abspath(filename)
directory, file = os.path.split(abs_path)
_, directory = os.path.split(directory)
dropbox_path = os.path.join(directory, file)
with open(abs_path, "rb") as f:
dbx.files_upload(f.read(), dropbox_path, mute=True)
link = dbx.files_get_temporary_link(path=dropbox_path).link
return link
你有 /home/user/project/file.txt
并且你需要 /project/file.txt
我会根据 os 默认分隔符进行拆分(因此它也适用于 windows 路径),然后仅使用正确的格式 (sep+path) 重新格式化最后两个部分,并且加入那个。
import os
#os.sep = "/" # if you want to test that on Windows
s = "/home/user/project/file.txt"
path_end = "".join(["{}{}".format(os.sep,x) for x in s.split(os.sep)[-2:]])
结果:
/project/file.txt
我将文件上传到保管箱 api,但它 post 在保管箱中从根文件夹开始我计算机上的所有目录。我的意思是你的项目文件夹在文件夹 home 中,而不是用户,直到你转到文件 sours 文件夹。如果我切割那个结构库就看不到它是文件,而不是字符串并给出错误信息。 我的代码是:
def upload_file(project_id, filename, dropbox_token):
dbx = dropbox.Dropbox(dropbox_token)
file_path = os.path.abspath(filename)
with open(filename, "rb") as f:
dbx.files_upload(f.read(), file_path, mute=True)
link = dbx.files_get_temporary_link(path=file_path).link
return link
有效,但我需要类似的东西:
file_path = os.path.abspath(filename)
chunks = file_path.split("/")
name, dir = chunks[-1], chunks[-2]
这让我犯了这样的错误:
dropbox.exceptions.ApiError: ApiError('433249b1617c031b29c3a7f4f3bf3847', GetTemporaryLinkError('path', LookupError('not_found', None)))
如何在路径中只设置父文件夹和文件名?
例如,如果我有
/home/user/project/file.txt
我需要
/project/file.txt
我假设以下代码应该有效:
def upload_file(project_id, filename, dropbox_token):
dbx = dropbox.Dropbox(dropbox_token)
abs_path = os.path.abspath(filename)
directory, file = os.path.split(abs_path)
_, directory = os.path.split(directory)
dropbox_path = os.path.join(directory, file)
with open(abs_path, "rb") as f:
dbx.files_upload(f.read(), dropbox_path, mute=True)
link = dbx.files_get_temporary_link(path=dropbox_path).link
return link
你有 /home/user/project/file.txt
并且你需要 /project/file.txt
我会根据 os 默认分隔符进行拆分(因此它也适用于 windows 路径),然后仅使用正确的格式 (sep+path) 重新格式化最后两个部分,并且加入那个。
import os
#os.sep = "/" # if you want to test that on Windows
s = "/home/user/project/file.txt"
path_end = "".join(["{}{}".format(os.sep,x) for x in s.split(os.sep)[-2:]])
结果:
/project/file.txt