2

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 Answers1

0

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]
  • thanks for your detailed answer. I am not a programmer, trying to learn python, you answer was very useful for me. i fixed the problem. – user7528506 Feb 09 '17 at 11:46
  • @user7528506 one does not usually come across the linecache module by accident; did you follow some advice on Stack Overflow? In that case I might comment there that `linecache` shouldn't be used for reading lines from a file whose contents are expected to change during the program's execution. – Antti Haapala -- Слава Україні Feb 09 '17 at 11:53
  • probably i found it somewhere but long time ago, can't remember the source. I used it in my old script, copied it from there at this time. – user7528506 Feb 09 '17 at 12:11