Question

I would like to give IronPython and Mono a try. Specifically doing sysadmin tasks. Which often means running OS commands. In CPython I use the subprocess module for such tasks. But in IronPython (v2.0.1, Mono 2.4, Linux) there is no subprocess module. It seems there is not even an 'os' module. So I can't use os.system(). What would be the IronPython way of doing tasks you would normally use 'subprocess' or 'os.system()' for in CPython?

Was it helpful?

Solution

I have found an answer. Thanks to the "IronPython Cookbook". One can find more information on this subject there: http://www.ironpython.info/index.php/Launching_Sub-Processes

>>> from System.Diagnostics import Process
>>> p = Process()
>>> p.StartInfo.UseShellExecute = False
>>> p.StartInfo.RedirectStandardOutput = True
>>> p.StartInfo.FileName = 'uname'
>>> p.StartInfo.Arguments = '-m -r'
>>> p.Start()
True
>>> p.WaitForExit()
>>> p.StandardOutput.ReadToEnd()
'9.6.0 i386\n'
>>> p.ExitCode
0
>>> 

OTHER TIPS

You can use most of the standard os modules from within ironpython.

import sys
sys.path.append path('...pathtocpythonlib......')
import os

Consider this C# Interactive Shell too....not sure if it supports IronPhython in the shell, but Mono does as you know.

There is a partial subprocess module implementation here:

http://www.bitbucket.org/jdhardy/code/src/tip/subprocess.py

The module (at this time, June 2010) is only supports redirecting STDIO pipes (as in, you cannot provide your own file-like objects to be filled with output or to stream intput), but the basics are enough to get by.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top