Python Forum

Full Version: Upload file to Box
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm trying to upload a csv file to Box via Mulesoft API, I can upload the file but only the file with last line, I can not upload full file. I m not even getting error.

Please see the following code.
import urllib
import requests

filePath='C:\\Work\\PNC\\Data\\'
fileName= 'CHASE.20171003.csv'
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
url = 'https://box-esb.cloudhub.io/uploadCHASEFile'
fileStream = open(filePath+fileName,'rb')

payload = {'Body':fileStream,'Name':fileName}
r=requests.post(url = url, headers=headers, data = payload)
fileStream.close()
print r
Please advice whats wrong in this code.

Thanks
Question 1: Why are you using python 2.7, latest version is 3.6.3, and 2.7 will only be maintained for a few more years.
try changing:
fileStream = open(filePath+fileName,'rb')
for l in fileStream:
    print l
to:
with open(filePath+fileName,'rb') as filestream:
    for line in fileStream.readlines():
        print(line)
Thanks Larz60+,

for now we are using 2.7 but may change in future...

Im not printing lines .. just open file and sending to upload.

fileStream = open(filePath+fileName,'rb')
 
payload = {'Body':fileStream,'Name':fileName}
r=requests.post(url = url, headers=headers, data = payload)
fileStream.close()

Please advise
You were printing in the first post!
review your edits