I cut a part of the script that i tried to complete. I am expecting a new result for mylineS.split()[0] after each iteration. outS.txt and outT.txt is result of commandC for each iteration and result is different at each iteration. But mylineS.split()[0] brings the first result for each iteration.
I guess something wrong with my approach, any idea?
B = 0
while B < len(Source_Tdevs):
devS = Source_Tdevs[B]
devT = Target_Tdevs[B]
subprocess.run(commandC, shell=True)
print (devS)
with open('outS.txt', 'r') as gS:
CS = len(gS.readlines())
mylineS = linecache.getline('outS.txt', CS -1)
Source_Tdevs_SGs.append(mylineS.split()[0])
**print (mylineS.split()[0])**
gS.close()
with open('outT.txt', 'r') as gT:
CT = len(gT.readlines())
mylineT = linecache.getline('outT.txt', CT - 1)
Target_Tdevs_SGs.append(mylineT.split()[0])
gT.close()
subprocess.run('del outS.txt, outT.txt', shell=True)
B= B + 1
commandC is one line above of subprocess.run(commandC, shell=True). I am writing bottom.
commandC = 'set "SYMCLI_OFFLINE=1" & set "SYMCLI_DB_FILE=C:\PROGRAM FILES\EMC\SYMAPI\DB\SYMAPI_DB.BIN" & call symaccess -sid %s list -type storage -dev %s > outS.txt & call symaccess -sid %s list -type storage -dev %s > outT.txt' % (
sid, devS, sid, devT)
You're abusing the
linecachemodule. The linecache is meant to be used for getting source code lines from Python source code:As the text implies the module will also retain the contents of the file in memory, and thus give correct output only on the first run. An easy remedy would be to invalidate the cache by using
Though a better thing to do would be to not use the linecache at all (it is not meant for this, and your files are constantly changing after all); instead just read all lines in using
.readlines()and extract the last with[-1], for example: