Python Forum
Upload file to Box - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Upload file to Box (/thread-5603.html)



Upload file to Box - skkuchipudi - Oct-12-2017

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


RE: Upload file to Box - Larz60+ - Oct-12-2017

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)



RE: Upload file to Box - skkuchipudi - Oct-13-2017

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


RE: Upload file to Box - Larz60+ - Oct-13-2017

You were printing in the first post!
review your edits