os.system在一个循环中

时间:2014-05-21 02:57:27

标签: python-2.7 os.system

对于列表中的每个项目,我正在修改一个文件,然后我想运行python脚本," ncall.py",它导入名为" new_basketparams.txt&的修改后的文件" new_basketparams.txt& #34 ;.我还想确保在调用之前ncall.py完成其进程,但这可能是另一个主题。我正在使用python 2.7的ubuntu linux机器上工作。

问题在于:我的代码似乎无限次地调用ncall.py,卡在循环内部。我甚至无法使用键盘快捷键来终止进程,必须关闭窗口才能终止进程。这是我的代码,如果有人可以帮助解释解决方案,或指导我,那将非常感激:

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

以下是一些诊断信息。导入MySQL错误很奇怪,因为ncall在终端自己调用时有效 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

现在我要包含ncall.py的一部分代码以及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

0 个答案:

没有答案