【发布时间】:2017-10-12 22:00:54
【问题描述】:
为什么 Python 2.7 os.system(command) 有时会在 Windows 8 中递归,而 os.startfile(command) 不递归?
我的命令是backup.bat,这是一个 Windows 批处理文件。
backup.bat的内容是:
"C:\Users\Frank Chang\Anaconda2\python.exe" -m animation_mini
@echo off
echo %time%
timeout 10 > NUL
echo %time%
我发现animation_mini.py 中的Python 2.7 animate 函数在os.system 时被多次调用的方式
使用的是在animate函数入口点的开头放置一个打印语句,并在控制台中统计打印语句。
今天有人告诉我 Python 2.7 os.system(command) 是 C 函数 execve 的包装器。但是这个事实并不能解释我在os.system('backup.bat') 中看到的递归。
os.system 是从 adder.cgi 调用的,这是一个 Python 2.7 CGI 脚本,其代码行是:
#!C:\Users\Frank Chang\Anaconda2\python.exe
import cgitb
import cgi
import os
import signal
import threading , time
import sys
sys.path.insert(0,"C:\Users\Frank Chang\Documents\Arduino\mary\data\usr\lib\python2.7\dist-packages\HTMLgen")
import HTMLgen
import subprocess
import win32api
import pandas as pd
def main():
form = cgi.FieldStorage()
numStr1 = form.getfirst("input1", "0")
numStr2 = form.getfirst("input2", "0")
numStr3 = form.getfirst("input3", "0")
numStr4 = form.getfirst("input4", "0")
numStr5 = form.getfirst("input5", "0")
numStr6 = form.getfirst("input6", "0")
numStr7 = form.getfirst("input7", "0")
numStr8 = form.getfirst("input8", "0")
numStr9 = form.getfirst("input9", "0")
numStr10 = form.getfirst("input10", "0")
numStr11 = form.getfirst("input11", "0")
numStr12 = form.getfirst("input12", "0")
numStr13 = form.getfirst("input13", "0")
numStr14 = form.getfirst("input14", "0")
from pandas import ExcelWriter
writer = ExcelWriter('PythonExport.xlsx')
from pandas import DataFrame
yourdf = DataFrame({'DC Start': numStr1, 'DC Duration': numStr2, 'Plant Start': numStr3, 'Plant Duration': numStr4,
'Supplier Start': numStr5, 'Supplier Duration': numStr6}, index=[0])
yourdf.to_excel(writer,'Disruptions')
yourdf = DataFrame({'FGI': numStr10, 'WIP': numStr11, 'DC': numStr12, 'Plant': numStr13,
'Supplier' : numStr14}, index=[0])
yourdf.to_excel(writer,'Policy')
writer.save()
os.system('backup.bat')
def processInput(numStr1, numStr2):
'''Process input parameters and return the final page as a string.'''
num1 = int(numStr1) # transform input to output data
num2 = int(numStr2)
total = num1+num2
return str(total)
def fileToStr(fileName):
"""Return a string containing the contents of the named file."""
fin = open(fileName);
contents = fin.read();
fin.close()
return contents
main()
我的 CGI 脚本可能是 os.system('backup.bat') 递归的原因吗?
【问题讨论】:
-
你能澄清一下这里的“递归”是什么意思吗?当它没有按您预期的方式工作时,实际发生了什么?你怎么知道它在做什么?
-
您从哪里运行
os.system命令?是在animation_mini.py文件中,还是在其他脚本中?能否展示相关的 Python 代码(而不是不太相关的批处理脚本代码)? -
os.system不是execve的包装。它调用system,在Windows上通过cmd.exe /c command执行命令。 -
@Blckknght,我可能在这里松散地使用“recurse”关键字。我的意思是,当我查看 CMD 窗口中的打印流活动查看 Python 2.7 animation_mini.py 代码时,python 2.7 animate() 函数在 4 分钟的时间内至少被调用了 4 次。我希望它应该只被调用一次,就像 os.startfile('backup.bat') 一样。
-
@FrancisTuan,
os.startfile将在新控制台中运行批处理文件,当附加的 cmd 和 Python 实例退出时,该批处理文件将被销毁。您是否有某种 cmdpause或 Pythoninput语句来保持控制台打开以实际查看它是否通过os.startfile多次运行?如果没有,请在批处理文件末尾添加pause语句。
标签: python windows python-2.7 recursion