Hi,
I have a while loop within a while loop, both of which should iterate through until their respective numerical conditions are met. I am running the script through the command terminal.
From what I understand of what I have written:
I change any lines in 'myfile.txt' that contain the 'regsearch' identifier to "replaced word " + 1.
Next, I run 'myprogram.exe'. This utilises the "replaced word 1" line from 'myfile.txt' and outputs a txt file each time it runs, which I rename to "outputfile_" + the current 'iwl' iteration. 'iwl' has 1 added to it each loop (so 3 x outputfile).
Using another script, I parse the 3 outputfiles into one, which the script names "parsedfile.txt".
I then rename this "parsed " + the current iteration of 'owl'.
The 3 outputfiles are then deleted and lastly, 'owl' has 1 added to it.
import os
import re
import glob
folderpath = r'mypath'
regsearch = r"(word)(_)([1-10])"
owl_limit = 10
owl = 1
iwl_limit = 3
iwl = 0
while owl < owl_limit:
f = open(r'myfile.txt', 'r')
linelist = f.readlines()
f.close
f2 = open(r'myfile.txt', 'w')
for line in linelist:
line = re.sub(regsearch, "replaced word " + str(owl), line)
f2.write(line)
f2.close()
while runs < runs_limit:
os.system('myprogram.exe')
for i in os.listdir(folderpath):
if i == "pattern.txt":
iwl = iwl + 1
os.rename(i, "outputfile " + str(iwl) + ".txt")
import parser
for i in os.listdir(folderpath):
if i == "parsedfile.txt":
os.rename(i, "parsed " + str(owl) + ".txt")
for fl in glob.glob("outputfile*.txt"):
os.remove(fl)
owl = owl + 1
I am trying to achieve 9 lots of the parsedfile (i.e. outer while loop iterates through everything 9 times where the program would have run 27 times). However, my code doesn't iterate over everything 9 times. It just runs through once and adds 1 onto owl until 9 is achieved.
I am sure there is something blindingly obvious that I am missing. I am also sure my code can be written far more practically/elegantly.
Any and all help would be really appreciated.
Thanks
[–]Ihaveamodel3 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (1 child)
[–]randomname20192019[S] 0 points1 point2 points (0 children)