Python-将csv文件上传到Dropbox

问题描述 投票:0回答:1

如何使用Python将csv文件上传到Dropbox


我在下面的文章中尝试了所有示例,但均无效

upload file to my dropbox from python script

我遇到错误:

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'



python file-upload dropbox-api
1个回答
0
投票
错误消息表明您正在提供'User \ pb \ Automation \ test.csv'的本地路径,但在本地文件系统上的该路径中找不到任何内容。

根据路径格式,看起来好像您在macOS上,但是访问主文件夹的路径错误。该路径应以“ /”开头,并且主文件夹位于“用户”(不是“用户”)下,因此您的folder定义应该为:

folder = pathlib.Path("/Users/pb/Automation")

或者,使用pathlib.Path.home()为您自动展开主文件夹:

pathlib.Path.home()

© www.soinside.com 2019 - 2024. All rights reserved.