python - Python - 将 csv 文件上传到 Dropbox
问题描述
如何使用 Python 将 csv 文件上传到 Dropbox
我尝试了下面这篇文章中的所有示例,均无效
我收到错误:
FileNotFoundError:[Errno 2] 没有这样的文件或目录:'User\pb\Automation\test.csv'
- 我的用户名:pb
- 文件夹名称:自动化
- 文件名:test.csv
import pathlib
import dropbox
import re
# the source file
folder = pathlib.Path("User/pb/Automation") # located in folder
filename = "test.csv" # file name
filepath = folder / filename # path object, defining the file
# target location in Dropbox
target = "Automation" # the target folder
targetfile = target + filename # the target path and file name
# Create a dropbox object using an API v2 key
token = ""
d = dropbox.Dropbox(token)
# open the file and upload it
with filepath.open("rb") as f:
# upload gives you metadata about the file
# we want to overwite any previous version of the file
meta = d.files_upload(f.read(), targetfile, mode=dropbox.files.WriteMode("overwrite"))
# create a shared link
link = d.sharing_create_shared_link(targetfile)
# url which can be shared
url = link.url
# link which directly downloads by replacing ?dl=0 with ?dl=1
dl_url = re.sub(r"\?dl\=0", "?dl=1", url)
print (dl_url)
FileNotFoundError: [Errno 2] No such file or directory: 'User\\pb\\Automation\\test.csv'
解决方案
错误消息表明您正在提供“User\pb\Automation\test.csv”的本地路径,但在本地文件系统上的该路径上没有找到任何内容。
根据路径格式,您似乎在 macOS 上,但您访问主文件夹的路径错误。路径应以“/”开头,主文件夹位于“用户”(而不是“用户”)下,因此您的folder
定义可能应该是:
folder = pathlib.Path("/Users/pb/Automation")
或者,用于pathlib.Path.home()
自动为您展开主文件夹:
pathlib.Path.home() / "Automation"
推荐阅读
- javascript - 如何使用本机共享 API 从 iOS Safari 网页共享图像?
- git - 一个工作文件夹可以同时属于 SVN 和 git 存储库吗?
- python - 散景在新标签中打开,但没有情节
- python - python中的isspace函数
- c++ - 在 C++ 中使用 while 循环时忽略换行
- java - 从存储过程返回两个用户定义类型对象并使用 spring-data-jpa 在存储库级别调用存储过程
- angular - “typeof Observable”类型上不存在属性“interval”
- r - 在背景ggplot2中将连续变量绘制为颜色渐变
- reactjs - axios调用后需要刷新页面
- r - R中散点图上两点之间的显着性标签