How to add loop to handle multiple object and multiple file in Python? -


first of all, new python.
had question , answer here.
form sloth's answer, got code handle single file , single object:

import re  # so, we're looking object 'heythere' objectname = 'heythere'  open('input.txt', 'r+') f:     line = f.readline()     pos = f.tell()     found = false     while line:          # want alter part          # right objectalias, use 'found' flag         if 'objectalias ' + objectname in line:             found = true         if 'endobject' in line:             found = false          if found , 'beginkeyframe' in line:              # found keyframe part, read lines              # until endkeyframe , count each line 'default'             sub_line = f.readline()             frames = 0             while not 'endkeyframe' in sub_line:                 if 'default' in sub_line:                     frames += 1                 sub_line = f.readline()              # since want override 'beginkeyframe',             # have move in file before line             f.seek(pos)              # read rest of file, skip             # old 'beginkeyframe' line want replace             f.readline()             rest = f.read()              # jump right position again             f.seek(pos)              # , write our new 'beginkeyframe' line             f.write(re.sub('\d+', str(frames), line, count=1))              # , write rest of file             f.write(rest)             f.truncate()             # nothing here anymore, quit loop             break          # before reading new line, keep track         # of our current position in file         pos = f.tell()         line = f.readline() 

due lack of programming practice, can't finish code handle multiple object , multiple file.
change objectname list of objects , add code below "while line", code don't work anymore, this:

objectlist = ['goodmoring', 'goodafternoon'] ...      while line:         while objectlist:             if 'objectalias ' + objectlist in line:             ... 

i know basic question, sorry that, still have ask question: how make code handle multiple object , multiple input file?

to check multiple objects, can try below

for objectname in objectlist:     if 'objectalias ' + objectname in line:         found = true 

or better 1 use any, any return true if element of iterable true

found = any('objectalias ' + objectname in line objectname in objectlist) 

full code below

objectlist = ['goodmoring', 'goodafternoon']  # can add loop here list of file process # ff in os.listdir(): #       open(ff, 'r+') f open('input.txt', 'r+') f:     line = f.readline()     pos = f.tell()     found = false     while line:         # if found true skip checking         found = found or any('objectalias ' + objectname in line objectname in objectlist)         if 'endobject' in line:             found = false          if found , 'beginkeyframe' in line:             # found keyframe part, read lines              # until endkeyframe , count each line 'default'             sub_line = f.readline()             frames = 0             while not 'endkeyframe' in sub_line:                 if 'default' in sub_line:                     frames += 1                 sub_line = f.readline()              pos2 = f.tell() # save pos continue next keyframe if there              # pos2 should store position of endkeyframe              # since want override 'beginkeyframe',             # have move in file before line             f.seek(pos)              # skip read rest of file coz wanna continue next keyframe              # , write our new 'beginkeyframe' line             f.write(re.sub('\d+', str(frames), line, count=1))              f.seek(pos2) # restore pos because wanna continue process next leyfram if there               # skip write rest of file             # don't truncate , break here bcoz wanna continue          # before reading new line, keep track         # of our current position in file         pos = f.tell()         line = f.readline() 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -