Running a batch file with parameters in Python OR F#

白昼怎懂夜的黑 提交于 2019-12-19 09:21:12

问题


I searched the site, but I didn't see anything quite matching what I was looking for. I created a stand-alone application that uses a web service I created. To run the client I use:

C:/scriptsdirecotry> "run-client.bat" param1 param2 param3 param4

How would I go about coding this in Python or F#. It seems like it should be pretty simple, but I haven't seen anything online that quite matches what I'm looking for.


回答1:


Python is similar.

import os
os.system("run-client.bat param1 param2")

If you need asynchronous behavior or redirected standard streams.

from subprocess import *
p = Popen(['run-client.bat', param1, param2], stdout=PIPE, stderr=PIPE)
output, errors = p.communicate()
p.wait() # wait for process to terminate



回答2:


In F#, you could use the Process class from the System.Diagnostics namespace. The simplest way to run the command should be this:

open System.Diagnostics
Process.Start("run-client.bat", "param1 param2")

However, if you need to provide more parameters, you may need to create ProcessStartInfo object first (it allows you to specify more options).




回答3:


Or you can use fsi.exe to call a F# script (.fsx). Given the following code in file "Script.fsx"

#light

printfn "You used following arguments: "
for arg in fsi.CommandLineArgs do
  printfn "\t%s" arg

printfn "Done!"

You can call it from the command line using the syntax:

fsi --exec .\Script.fsx hello world

The FSharp interactive will then return

You used following arguments:
        .\Script.fsx
        hello
        world
Done!

There is more information about fsi.exe command line options at msdn: http://msdn.microsoft.com/en-us/library/dd233172.aspx



来源:https://stackoverflow.com/questions/2916758/running-a-batch-file-with-parameters-in-python-or-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!