0

I am trying to upload file from my desktop to SharePoint using python script. I am getting the error "xml.sax._exceptions.SAXparseException no element found"... Please suggest a solution as I am pretty new to python and all my attempt has failed thus far. The script looks as follows,

#!/usr/bin/python2.4

import datetime as dt
import sys
from suds.transport.https import WindowsHttpAuthenticated
from suds.sax.element import Element
from suds.sax.element import Attribute
from suds import client
from ntlm import HTTPNtlmAuthHandler
import urllib2
import os.path

FOLDER = dt.date.today().strftime("%Y-%m-%d")  #folder name that will be created
FNAME = "Testfile"                          #file name to upload
SITE = "My sharepoint path"
FURL = "%s/Reports/%s/%s" % (SITE,FOLDER,os.path.basename(FNAME))
USER = "Domain\\UserID"   # AD user name
PASS = "Password"

def main():
 wss_lists = client.Client("%s/_vti_bin/lists.asmx?WSDL" %                         SITE,transport=WindowsHttpAuthenticated(username=USER,password=PASS))
 wss_dws = client.Client("%s/_vti_bin/dws.asmx?WSDL" % SITE,transport=WindowsHttpAuthenticated(username=USER,password=PASS))
 wss_dws.service.CreateFolder("Reports/%s" % FOLDER)
  print uploadReport(FURL,FNAME)


def uploadReport(furl,fname):
  pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
  pm.add_password(None,SITE,USER,PASS)
  op = urllib2.build_opener(HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(pm))
  import pdb;pdb.set_trace()
  fh = open(fname)
  data = fh.read()
  fh.close()
  req = urllib2.Request(furl,data=data)
  req.get_method = lambda: 'PUT'
  req.add_header('Content-Type','txt')
  r = op.open(req)enter code here
  return r.read()

if __name__=="__main__": main()
Shobhit
  • 15
  • 3

2 Answers2

0

Code seems to suggest that it uploads a file read from disk only, so the XML you're trying to upload might not be well-formed. I'd start with checking that.

Another issue is that 2.4 is really old. Install 2.7 if you can, just to avoid old bugs to bite you.

Then you can also do nice things like:

with open('Testfile') as fileobj:
   content = fileobj.read()

And after execution leaves with block fileobj will be closed automatically.

LetMeSOThat4U
  • 6,470
  • 10
  • 53
  • 93
0

I found a work around.... I am creating a "Map Drive"... Once the Map Drive is created its just copy and paste, if u copy a file to a map drive then it is automatically reflected on sharepoint..

You can create a Map Drive manually. Alternatively, For creating Map drives using script, refer to following link :-

What is the best way to map windows drives using Python?

Community
  • 1
  • 1
Shobhit
  • 15
  • 3