something wrong with with open statement which is in while loop

152 Views Asked by At

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)
1

There are 1 best solutions below

3
Antti Haapala -- Слава Україні On BEST ANSWER

You're abusing the linecache module. The linecache is meant to be used for getting source code lines from Python source code:

The linecache module allows one to get any line from a Python source file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. This is used by the traceback module to retrieve source lines for inclusion in the formatted traceback.

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

linecache.checkcache('outS.txt')

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:

lines = gS.readlines()
last_line = lines[-1]