Python 无法将字符串值更改为具有字符串值的变量

Python 无法将字符串值更改为具有字符串值的变量,python,python-3.x,dropbox-api,Python,Python 3.x,Dropbox Api,我将文件上传到dropbox api,但它会在dropbox上发布我的计算机自根文件夹以来的所有目录。我的意思是,你们有你们的项目文件夹内的文件夹主页,而不是用户,直到你们去文件sours文件夹。若我剪切那个结构库,就看不到它是文件,而不是字符串,并给出错误消息。 我的代码是: def upload_file(project_id, filename, dropbox_token): dbx = dropbox.Dropbox(dropbox_token) file_path =

我将文件上传到dropbox api,但它会在dropbox上发布我的计算机自根文件夹以来的所有目录。我的意思是,你们有你们的项目文件夹内的文件夹主页,而不是用户,直到你们去文件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

我假设以下代码应该可以工作:

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

我会根据操作系统默认的分隔符进行拆分(这样它也可以与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

您有
/home/user/project/file.txt
,需要
/project/file.txt

我会根据操作系统默认的分隔符进行拆分(这样它也可以与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

在分割前,
文件路径的值是多少?像home/user/project/file.txt这样的东西,但我只需要/project/file.txt,那么你可能不需要传递上面所有的信息,只需要你的问题来解释:我有这个,我想要那个,我试过那个,那个给了我这个。。。更简短、更清晰。
file\u path
的值是什么?像home/user/project/file.txt这样的东西,但我只需要/project/file.txt那么你可能不需要传递上面所有的信息,只要你的问题来解释:我有这个,我想要那个,我试过那个,那个给了我这个。。。更短,更清晰。