0

For each item in a list, I am modifying a file and then I want to run the python script, "ncall.py", which imports the modified file called, "new_basketparams.txt". I also want to make sure ncall.py completes its process before recalling it, but that is perhaps another topic. I am working on an ubuntu linux machine with python 2.7.

Here's the issue: My code seems to call ncall.py infinite times, stuck inside of the loop. I cannot even use a keyboard shortcut to kill the process and must close the window to kill the process. Here is my code and if anyone can help explain a solution, or guide me, that would be greatly appreciated:

import os
import re
datelist=['2014-05-16','2014-05-15','2014-05-14','2014-05-13','2014-05-12']
for date in datelist:
    f=open('basketparams.txt')
    g=open('new_basketparams.txt','w')
    for line in f:
        if "Start" in line:
            line=line.split(";")
            line[2]=re.sub('\d\d\d\d-\d\d-\d\d',date,line[2])
            line=';'.join(line)
        elif "End" in line:
            line=line.split(";")
            line[2]=re.sub('\d\d\d\d-\d\d-\d\d',date,line[2])
            line=';'.join(line)
        else:
            pass
        g.write(line)
    os.system("python ncall.py")

Here are some diagnostics. The import MySQL error is strange because ncall works when called by itself from the terminal diagnostics in code

'import site' failed; use -v for traceback
Traceback (most recent call last):
  File "ncall.py", line 1, in <module>
    import MySQLdb
ImportError: No module named MySQLdb
[]
calling ncall.py no. 1  2014-05-21 07:30:46.400011
new_basketparams.txt
Traceback (most recent call last):
  File "ncall.py", line 15, in <module>
    Price_Min=display.input[2]
IndexError: list index out of range
[]
calling ncall.py no. 1  2014-05-21 07:30:46.352573
new_basketparams.txt
Traceback (most recent call last):
  File "ncall.py", line 15, in <module>
    Price_Min=display.input[2]
IndexError: list index out of range
[]
calling ncall.py no. 1  2014-05-21 07:30:46.305043
new_basketparams.txt
Traceback (most recent call last):
  File "ncall.py", line 15, in <module>
    Price_Min=display.input[2]
IndexError: list index out of range

Now I am including a portion of code from ncall.py as well as display.py:

#ncall.py
import MySQLdb
import csv
import display
import time
from display import fileinput
from datetime import datetime, date, time, timedelta
import datelist
now0=datetime.now()
print fileinput

Start_Datetime='%s 14:00:00'%(datelist.date)
End_Datetime='%s 14:59:59'%(datelist.date)
Price_Min=display.input[2]
Price_Max=display.input[3]
Under=display.input[4]

#display.py
fileinput='new_basketparams.txt'
f=open(fileinput)
input=[]
for line in f:
    print 'reading line in fileinput in display.py\'s loop: ', line
    try:
     line=line.split(';')
     parameter=line[0]
     Input=line[2]

     x=Input.split('\r')
     input.append(x[0])

    except :
        continue
teachamantofish
  • 75
  • 1
  • 1
  • 7
  • I don't see anything in that code that suggests an infinite loop. Are you sure that's what is happening? How can you tell? Does `ncall` have distinctive output that you can see? Can you add diagnostics to the loop, like `print "calling ncall"` just before the `os.system` call? – Blckknght May 21 '14 at 03:45
  • I included a "calling ncall" line, plus a break in the code and directed the console output to a .txt file for some analysis. now I'm pasting the diagnostics here... – teachamantofish May 21 '14 at 12:40
  • as you can see, it might be tripping up on the IndexError?? But I am not sure why it would continue to call ncall.py – teachamantofish May 21 '14 at 12:51
  • I think you will need to show us more code. The infinite loop is not in the code you've shown so far, but perhaps in whatever is calling it. – Blckknght May 21 '14 at 13:26
  • I have provided some more code. I am curious if the issue revolves around ncall.py importing datelist, the module which called ncall.py in the first place. – teachamantofish May 21 '14 at 14:55

0 Answers0