On Fri, Jan 22, 2016 at 1:48 PM, Joel Goldstick <joel.goldst...@gmail.com> wrote:
> > > On Fri, Jan 22, 2016 at 1:16 PM, Sam Starfas via Tutor <tutor@python.org> > wrote: > >> >> >> >> Hi,I am new to Python, but learning. (Hopefully the layout is readable) >> I am having trouble getting two scripts to work together. What I want to >> do is have the combinded script do the following: >> 1. Read through a directory of xml files, not a single file, but many xml >> files.2. Read each files contents.3. Pull out the <uicontrol> element >> content (words between open and close tag) >> * For example: <uicontrol>Sam was here.</uicontrol> >> * The script would pull out the content "Sam was here" and print it to >> a file. >> >> My scripts so >> far:----------------------------------------------------------Script to get >> <uicontol> >> content----------------------------------------------------------import >> xml.etree.ElementTree as ET >> tree = ET.parse('TEST.xml') >> root = tree.getroot()for uicontrol in root.iter('uicontrol'): >> print(uicontrol.text) >> > > Make the above a function > def get_uicontrol(f): > >> tree = ET.parse(f)) >> root = tree.getroot()for uicontrol in root.iter('uicontrol'): >> print(uicontrol.text) >> > > ----------------------------------------------------------Script to read >> through the >> files:----------------------------------------------------------import sys >> import glob >> import errnopath >> ='/Users/sastarfa/Documents/Projects/Script-projects/Scripts/Pull-out-terms-between-elements/*.xml' >> files = glob.glob(path) >> for name in files: # 'file' is a builtin type, 'name' is a >> less-ambiguousvariable name. >> try: >> with open(name) as f: # Noneed to specify 'r': this is the >> default. >> sys.stdout.write(f.read()) >> >> > Make the above a function: > > def get_files(path): > glob.glob(path) > for name in files: > try: > with open(name) as f: > get_uicontrol(f) > > oops, edited above two lines > then at the end of your file do this: > > if __name__ == '__main__': > get_files('/your/path/goes/here') > > get_files will loop to open each file, then call get_uicontrol on that > open file. get_uicontrol will find your tag, collect the text and print it > > I haven't tested this, but it is at least close > > >> This is the area I get stuck. I can understand, write a script to run on >> one a single file you input into the script, but getting a script to run on >> a directory I am not understanding how to do this. >> Thanks in advance for any and all help.Really appreciate the help. >> Sam >> >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > > > -- > Joel Goldstick > http://joelgoldstick.com/stats/birthdays > -- Joel Goldstick http://joelgoldstick.com/stats/birthdays _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor