我想用启动命令运行我的python程序(不是脚本),这是要执行的python程序文件中的一种方法

时间:2018-12-19 06:45:20

标签: python cmd

您好,其中有一个定义了启动方法的python程序,在启动方法中,我正在调用win32serviceutil.StartService(service)方法来启动服务,例如

import os, platform, subprocess

try:
    import win32serviceutil
except:
    os.system("pip install pywin32")
    os.system("pip install pypiwin32")
    import win32serviceutil

OS = platform.system() #will get you the platform/OS
print("You are using ", OS)

if __name__=='__main__':

    service = 'WSearch'
    def startByCLI():
        cmd = 'net start '+service
        os.system(cmd)

    def startByPython():
        # subprocess.check_output(["sc", "start", service], stderr=subprocess.STDOUT)
        win32serviceutil.StartService(service)

    if OS=='Windows':
        try:
            output = startByPython()
        except subprocess.CalledProcessError as e:
            print(e.output)
            print(e.returncode)
            #os.system('python test2.py')
            subprocess.call("python ./install.py asadmin", shell=True)
            startByCLI()

所以我真正想要的是要从命令promt这样运行start方法

python ./myfile.py startByPython

,它将触发startByPython中的myfile.py方法

非常感谢

1 个答案:

答案 0 :(得分:1)

谢谢大家的关注, 我想使用命令行中的参数来运行我的file.py文件,例如:

$ / usr / bin / python myfile.py start

我得到的解决方案是

  def main():
   # read arguments from the command line and 
   # check whether at least two elements were entered
   if len(sys.argv) < 2:
      print "Usage: python aws.py {start|stop}\n"
      sys.exit(0)
   else:
      action = sys.argv[1] 
   if action == "start":
      startInstance()
   elif action == "stop":
      stopInstance()
   else:
      print "Usage: python aws.py {start|stop}\n"