windows 在 Python 或 F# 中运行带参数的批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2916758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running a batch file with parameters in Python OR F#
提问by Ramy
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:
我搜索了该网站,但没有看到任何与我要查找的内容完全匹配的内容。我创建了一个使用我创建的 Web 服务的独立应用程序。要运行我使用的客户端:
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.
我将如何在 Python 或 F# 中进行编码。看起来它应该很简单,但我还没有在网上看到任何与我正在寻找的内容完全匹配的内容。
回答by gradbot
Python is similar.
Python 类似。
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
回答by Tomas Petricek
In F#, you could use the Process
class from the System.Diagnostics
namespace. The simplest way to run the command should be this:
在 F# 中,您可以使用命名空间中的Process
类System.Diagnostics
。运行命令的最简单方法应该是这样的:
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).
但是,如果您需要提供更多参数,则可能需要先创建ProcessStartInfo
对象(它允许您指定更多选项)。
回答by Huusom
Or you can use fsi.exe to call a F# script (.fsx). Given the following code in file "Script.fsx"
或者,您可以使用 fsi.exe 调用 F# 脚本 (.fsx)。鉴于文件“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
然后 FSharp 交互将返回
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
msdn 上有关于 fsi.exe 命令行选项的更多信息:http: //msdn.microsoft.com/en-us/library/dd233172.aspx