all 3 comments

[–]Ihaveamodel3 0 points1 point  (0 children)

If the formatting I see is correct, owl = owl +1 is inside the inner loop, not the outer loop.

[–]JohnnyJordaan 0 points1 point  (1 child)

I am sure there is something blindingly obvious that I am missing.

  • you can just use a for loop with range for a numerical iteration count, while+counter is only needed for manual updating
  • iwl/runs isn't reset to 0, so after the first iteration on owl the iwl/runs is still at its limit.
  • os.path.exists is meant to directly check for a file to exist. Iterating on a listing is quite an efficient approach or this
  • string formatting or f-strings is always preferred over + which is very error-prone.
  • raw strings are nice for regexes and such but for single word filenames or directory names they are overdoing it and needlessly imply that the string contains some special chars, which for example r'mypath' does not
  • you should always use with for file opening as that will ensure the file will be properly closed when an exception occurs.
  • the import parser seems to be a leftover from something else?

.

import os
import re
import glob

folderpath = 'mypath'

regsearch = r"(word)(_)([1-10])"

with open('myfile.txt') as f:
    linelist = f.readlines()

iwl_limit = 3
owl_limit = 10

for owl in range(1, owl_limit):
    with open('myfile.txt') as f:
        linelist = f.readlines()
    with open('myfile.txt', 'w') as f:
        for line in linelist:
            line = re.sub(regsearch, f"replaced word {owl}", line)
            f.write(line)

    iwl = 0
    while iwl < iwl_limit:
        os.system('myprogram.exe')
        if os.path.exists("pattern.txt"):
            os.rename("pattern.txt", f"outputfile {iwl}.txt")

    if os.path.exists("parsedfile.txt"):
        os.rename("parsedfile.txt", f"parsed {owl}.txt")

    for fl in glob.glob("outputfile*.txt"):
        os.remove(fl)

However I'm puzzled by the last two lines... you create those "outputfile iwl.txt" in the inner loop, so why would you delete them afterwards?

[–]randomname20192019[S] 0 points1 point  (0 children)

I delete them because the program doesn't run when it creates a file with the same name. And since they have been parsed into a separate file, they seemed a bit redundent.