6

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
  • I really appreciate both answers. Right now, i'm closer to getting python to work. I don't see why i should pick one answer over another though and i don't have enough "reputation" to vote answers up. Commented May 27, 2010 at 21:33

3 Answers 3

15

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
Sign up to request clarification or add additional context in comments.

6 Comments

i'm close: Traceback (most recent call last): File "C:\Python Scripts\eodReports.py", line 14, in <module> p = subprocess.Popen([batch, clientID, username, password, output], stdout=subprocess.PIPE, stderr=subprocess.PIPE) File "C:\Python25\lib\subprocess.py", line 586, in init errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "C:\Python25\lib\subprocess.py", line 681, in _get_handles p2cread = self._make_inheritable(p2cread) File "C:\Python25\lib\subprocess.py", line 722, in _make_inheritable DUPLICATE_SAME_ACCESS) TypeError: an integer is required
Try setting stdin also even if you don't need it. p = subprocess.Popen([batch, clientID, username, password, output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) I believe this is fixed in python 2.6.
sorry there is such a delay between reponses. This is a side project so I don't have much time to work on it. See the exception below, please:
Traceback (most recent call last): File "C:\Python Scripts\eodReports.py", line 14, in <module> p = subprocess.Popen([batch, clientID, username, password, output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) File "C:\Python25\lib\subprocess.py", line 593, in init errread, errwrite) File "C:\Python25\lib\subprocess.py", line 750, in _execute_child args = list2cmdline(args) File "C:\Python25\lib\subprocess.py", line 502, in list2cmdline needquote = (" " in arg) or ("\t" in arg) TypeError: argument of type 'int' is not iterable
Maybe you should post your code as another question. It's hard for me to debug it just by the traceback.
|
8

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).

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.