dropboxdropbox-apidropbox-sdk

Read a CSV file on Dropbox without downloading it


I am developing an application where a user can just provide the path of there drop box file and access token and see the contents of the file. Is it possible to just read the contents of the file without actually downloading it.

import dropbox
import tempfile
import csv

dbx = dropbox.Dropbox(<access_token>)

metadata, f = dbx.files_download('/test/MOCK_DATA.csv')

filename = tempfile.NamedTemporaryFile(suffix='.csv').name

with open(filename, 'wb') as file:
  file.write(f.content)

with open(filename) as file:
  csv_reader = csv.reader(file, delimiter=',')
line_count = 0
for row in csv_reader:
  if line_count == 0:
    print(f'Column names are {", ".join(row)}')
    line_count += 1
  else:
    print(row)
    line_count += 1
print(f'Processed {line_count} lines.')

Currently i am able to read the contents but only after downloading it.


Solution

  • Using files_download is the right way to access file data using the Dropbox Python SDK. In your code you're then writing the file data to a local file, but that isn't required.

    The csv.reader method is documented as:

    csvfile can be any object which supports the iterator protocol and returns a string each time its next() method is called — file objects and list objects are both suitable.

    So you can do something like this:

    import dropbox
    import csv
    
    dbx = dropbox.Dropbox(<access_token>)
    
    metadata, f = dbx.files_download('/test/MOCK_DATA.csv')
    
    csv_reader = csv.reader(f.content.decode().splitlines(), delimiter=',')
    
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:
            print(row)
            line_count += 1
    
    print(f'Processed {line_count} lines.')