Python - загрузить CSV-файл в Dropbox - PullRequest
0 голосов
/ 01 октября 2019

Как загрузить CSV-файл в Dropbox с Python


Я попробовал все примеры в этом посте ниже, ни один из них не работает

загрузить файл в мой Dropbox из скрипта Python

Я получаю ошибку:

FileNotFoundError: [Errno 2] Нет такого файла или каталога: 'User \ pb \ Automation \ test.csv'


  • Мое имя пользователя: pb
  • Имя папки: Automation
  • Имя файла: 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'



1 Ответ

1 голос
/ 01 октября 2019

В сообщении об ошибке указывается, что вы указываете локальный путь 'User \ pb \ Automation \ test.csv', но по этому пути в вашей локальной файловой системе ничего не найдено.

На основе путиформат выглядит так, как будто вы работаете в macOS, но у вас неверный путь для доступа к вашей домашней папке. Путь должен начинаться с «/», а домашние папки расположены в разделе «Пользователи» (не «Пользователь»), поэтому ваше определение folder, вероятно, должно быть:

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

Или используйте pathlib.Path.home(), чтобы автоматически развернуть домашнюю папку для вас:

pathlib.Path.home() / "Automation"
...