0

How to upload csv file to Dropbox with Python


I tried all the examples in this post bellow, neither works

upload file to my dropbox from python script

I am getting error:

FileNotFoundError: [Errno 2] No such file or directory: 'User\pb\Automation\test.csv'


  • My username: pb
  • Folder name: Automation
  • file name: 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'



Peter
  • 544
  • 5
  • 20

1 Answers1

1

The error message is indicating that you're supplying a local path of 'User\pb\Automation\test.csv' but nothing was found at that path on your local filesystem.

Based on the path format, it looks like you're on macOS, but you have the wrong path for accessing your home folder. The path should start with "/", and the home folders are located under "Users" (not "User"), so your folder definition should probably be:

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

Or, use pathlib.Path.home() to automatically expand the home folder for you:

pathlib.Path.home() / "Automation"
Greg
  • 16,359
  • 2
  • 34
  • 44