开发者

Need help using python 2.7.1 to select and run various batch files based on user input

开发者 https://www.devze.com 2023-04-07 17:00 出处:网络
all.I am trying to use python 2.7.1 to run batch files based on what a user enters at the input prompt.The problem I\'m having is the batch files do not run.I have been able to get a specific batch fi

all. I am trying to use python 2.7.1 to run batch files based on what a user enters at the input prompt. The problem I'm having is the batch files do not run. I have been able to get a specific batch file to ru开发者_如何转开发n from python if it is called from main() but cannot get python to select the file based on the input value. My code is:

from subprocess import Popen


def results():
    Popen("results.txt")
    selection()

def buildinga():
    Popen("5463.bat")
    results()

def base():
    Popen("base.bat")
    results()

def selection():
    print "This tool will check the device status for the specified building."
    print "Type 'exit' to quit the program."
    selection.c = input("Please enter the device number: ")


    if selection.c == "001":
        base()

    if selection.c == "5463":
        buildinga()

    if selection.c == "exit":
        exit()



def main():
    selection()


main()

The way I can run a single batch file is:

from subprocess import Popen

def batchwriter():
    Popen("make.bat")

def main():
    batchwriter()

main()

This way does not allow me to select which batch file to run, only the one specified. Any help would be much appreciated.

I tried to post this in comments but could not. raw_input is working. The batch file is being run and then the results file displays, however, errors are displayed, as well. The errors don't seem to affect functionality because the results are correct. I am getting the following output:

This tool will check the device status for the specified building.
Type 'exit' to quit the program.
Please enter the device number: 5463
Traceback (most recent call last):
  File "M:\cstat\ct.py", line 37, in <module>
    main()
  File "M:\cstat\ct.py", line 34, in main
    selection()
  File "M:\cstat\ct.py", line 26, in selection
    buildinga()
  File "M:\cstat\ct.py", line 10, in buildinga
    results()
  File "M:\cstat\ct.py", line 5, in results
    Popen("results.txt")
  File "C:\Utilities\Python\lib\subprocess.py", line 672, in __init__
    errread, errwrite)
  File "C:\Utilities\Python\lib\subprocess.py", line 882, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

M:\cstat>[ 12:11:36.63] 5463 is ONLINE


Use raw_input istead of input. input converts numeric input to from string to integer.

>>> input()
001
1
>>> raw_input()
001
'001'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号