python变量传递给系统命令的方法

python程序内执行shell命令可以有几种方式,在http://www.cnblogs.com/xuxm2007/archive/2011/01/17/1937220.html 里都有详细介绍。

实际操作中经常遇到的情况是如何将变量输入到系统命令中。如下例所示:

      output='output.txt'
ra = 10
dec = 111
filter_command = 'select infile=data.txt outfile=%s ra=%s dec=%s rad=10.0 tmax="INDEF" emin=100.0 evtype=3'%(output,ra,dec) if os.system(filter_command) == 0: print 'successful filter ', name else: print 'failed filter ',name