pythonでコマンドライン引数に入ったコマンドを実行する

subprocesstest.py

import subprocess, sys, time
if __name__ == "__main__":
	if len(sys.argv) < 2: sys.exit()
	print "starting.",
	sys.stdout.flush()
	p = subprocess.Popen(sys.argv[1:], stdout=subprocess.PIPE)
	while( p.poll() == None ):
		sys.stdout.write('.')
		sys.stdout.flush()
		time.sleep(1)
	print "done"
	print p.stdout.read()

loop.py

import sys, string
def loop(n):
	cnt = 1.0001
	for i in range(0, n):
		cnt *= 1.0000023
	return cnt
if __name__ == "__main__":
	if len(sys.argv) > 1:
		r = string.atoi(sys.argv[1])
		if r > 7:
			print "radius larger than 8 is not recommended."
			print "your input is:", r
			sys.exit()
		else: pass
		n = 10 ** r
	else:
		n = 10 ** 7
	print "n=", n, "result=", loop(n)

実行結果:

% python subprocesstest.py  python loop.py
starting......done
n= 10000000 result= 9745520155.61

%

参考