Search code examples
pythonxmlpostgresqltornado

Reading n number of files from a folder for input using python and tornado


Hi I am trying to add values to my postgres database and the values to be inserted are in xml format in multiple files (since multiple data values). My script accepts only XML as input. Is there a way so that I can read data from all the files in that particular folder and send it to my database in the same XML format. using python and tornado.

Initially i did like this:

data = "<xml>the entire xml file content here </xml>"
content = document_create(student_id=req['student_id'], body =data)
self.write(content)

My xml looks like:(test1.xml)

<Students >
<name> Alex </name>
<no. of days present>20</no. of days present>
<no. of days absent></no. of days absent>
<subject>
.....
  <total>458</total>
</subject>
<staff>
Martin
</staff>
</Students>

For a list of 100 odd students.

Thanks


Solution

  • This will iterate over all xml in the directory and open xml files as data,

    import glob
    ListofFiles = glob.glob("/<Dir>/*.<xml or file format in folder>")
    for files in ListofFiles:
        with open(files,'r') as fl:
            content = fl.read()
            data = "<xml>%s<xml>" % content
            <your code>
            ...
    

    I am assuming your one xml will fit into memory at a time.