python - Cant change string value to variable with string value -
i upload file dropbox api, post on dropbox directories computer since root folder. mean have folder of project inside folder home, user until go file sours folder. if cut structure library can't see file, not string , give mistake message. code is:
def upload_file(project_id, filename, dropbox_token): dbx = dropbox.dropbox(dropbox_token) file_path = os.path.abspath(filename) open(filename, "rb") f: dbx.files_upload(f.read(), file_path, mute=true) link = dbx.files_get_temporary_link(path=file_path).link return link
it works, need like:
file_path = os.path.abspath(filename) chunks = file_path.split("/") name, dir = chunks[-1], chunks[-2]
which gives me mistake like:
dropbox.exceptions.apierror: apierror('433249b1617c031b29c3a7f4f3bf3847', gettemporarylinkerror('path', lookuperror('not_found', none)))
how make parent folder , filename in path?
for example if have
/home/user/project/file.txt
i need
/project/file.txt
you have /home/user/project/file.txt
, need /project/file.txt
i split according os default separator (so work windows paths well), reformat 2 last parts proper format (sep+path) , join that.
import os #os.sep = "/" # if want test on windows s = "/home/user/project/file.txt" path_end = "".join(["{}{}".format(os.sep,x) x in s.split(os.sep)[-2:]])
result:
/project/file.txt
Comments
Post a Comment