python - argument of type 'NoneType' is not iterable -


i trying open directory contains series of xml's in 1 specific directory.
in following code, iterating through each xml document, , i'm setting "if statements" read text in xml, find keywords , replace them , write new file new location.
getting following error when run script:

traceback info:   file "z:\esri\python\test scripts\elementtree6.py", line 62, in <module>     if "%begdate%" in element.text:         ... 

error info:

argument of type 'nonetype' not iterable 

i have hard coded directory 1 specific xml , when run through if statements, work fine.
it's when try set iterate through series of xml's run error.
searched through site see if final solution, issues either different mine or not quite understanding work-around.

i have used number of print lines test outputs.
works fine until if statement , error arises.

# location of xml's folderpath = r"z:\data"  # set variable store files extension ".xml" filename in glob.glob(os.path.join(folderpath, "*.xml")):  fullpath = os.path.join(folderpath, filename)  # find files , split filename directory path if os.path.isfile(fullpath):     basename, filename2 = os.path.split(fullpath)     #print "basename = " + basename     #print "filename = " + filename2      # set variable store xml structure xml file     root = elementtree(file=r"z:\data\\" + filename2)       #create iterator     iter = root.getiterator()     #iterate     element in iter:         #print element.text              if "%begdate%" in element.text:             begdate = element.text.replace("%begdate%", begdateparam)             element.text = begdate 

you find there many elements in xml document contain no character data (i.e. text). 1 of nice things python none false in conditional, coupling knowledge of how conditional statements short circuit means there's simple solution problem, change line:

if "%begdate%" in element.text: 

to:

if element.text , "%begdate%" in element.text: 

when there no text, element.text none, a.k.a false, , won't part of conditional causing error.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -