• jacob alex 6
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi,
I am trying to read the body of attachment and writing in a text file using Python. Below is the sample code. 
import urllib
import requests
import base64
import json
from simple_salesforce import Salesforce
import urllib.request as urllib



instance = ''

sf = Salesforce(username='username', password='password', security_token='security_token')
sessionId = sf.session_id

attachment = sf.query("SELECT id, name,Body FROM Attachment where parentID =''")
body = ""

req = urllib.Request('https://%s.salesforce.com/services/data/v38.0/sobjects/Attachment/<id>/Body/' % instance,
    headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % sessionId })
f = urllib.urlopen(req)
f1 = open("demofile.txt", "wb")
for x in f:
    f1.write(x)
f.close()
f1.close()
But, I am getting below error.
f1.write(x)
TypeError: write() argument must be str, not bytes
Please correct me, if something is wrong.

Thanks and Regards,
Diwakar G