searching and storing large quantities of xml!

dads wayne.dads.bell at
Mon Jan 18 22:39:14 CET 2010

Thanks all, took your advice and have been playing all weekend which
has been great fun. ElementTree is awesome. I created a script that
organises the xml as they're in year blocks and I didn't realise the
required xml is mixed up with other xml. Plus the volumes are much
greater than I realised, I checked as back at work and it was
something like 600,000 files in a year, just over a gig for each

I'm going to add zipping up of the files and getting the required info
and putting it in a db this week hopefully. It's been completely
overhauled, originally I used modified date now it gets the date from
the parsed xml, safer that way. The code is below but word of caution,
it's hobbyist code so it'll probably make your eyes bleed =), thanks

There was one thing that I forgot about - when ElementTree fails to
parse due to an element not being closed why doesn't it close the file
like object. As later on I would raise 'WindowsError: [Error
32] ...file being used by other process' when using shutil.move(). I
got round this by using a 'try except' block.

from __future__ import print_function
import xml.etree.cElementTree as ET
import calendar
import zipfile
import os.path
import shutil
import zlib
import os

class Xmlorg(object):

    def __init__(self):

        self.cwd = os.getcwd()
        self.year = os.path.basename(self.cwd)

    def _mkMonthAndDaysDirs(self):

        ''' creates dirs for every month and day of a of specidifed
            Works for leap years as well.


            ...2010/201001/03                 '''

        def addZero(n):

            if len(str(n)) < 2:
                return '0' + str(n)
                return str(n)

        dim = [ calendar.monthrange(year,month)[1] for year in \
        [int(self.year)] for month in range(1,13) ]

        count = 1
        for n in dim:
            month = addZero(count)
            count += 1
            ym = os.path.join(self.cwd, self.year + month)
            for x in range(1,n+1):
                x = addZero(x)
                os.mkdir(os.path.join(ym, x))

    def ParseAndOrg(self):

        '''requires dir and zip struct:

        .../(year)/(year).zip - example .../2008/ '''

        def movef(fp1,fp2):

            '''moves files with exception handling'''

            except IOError, e:
            except WindowsError, e:

        os.mkdir(os.path.join(self.cwd, 'otherFileType'))

        # dir struct .../(year)/(year).zip - ex. .../2008/
        zf = zipfile.ZipFile(os.path.join(self.cwd, self.year +
        ld = os.listdir(self.cwd)
        for i in ld:
            if os.path.isfile(i) and i.endswith('.xml'):
                    tree = ET.parse(i)
                    print('%s np' % i) #not parsed
                root = tree.getroot()
                if root.findtext('Summary/FileType') == 'Order':
                    date = root.findtext('OrderHeader/OrderDate')[:10]
                    dc = date.split('/')
                    fp1 = os.path.join(self.cwd, i)
                    fp2 = os.path.join(self.cwd, dc[2] + dc[1], dc[0])
                    fp1 = os.path.join(self.cwd, i)
                    fp2 = os.path.join(self.cwd, 'otherFileType')

if __name__ == '__main__':
    os.chdir('c:/sv_zip_test/2010/') #remove
    xo = Xmlorg()

More information about the Python-list mailing list