os.system()使用python执行的Ruby脚本

os.system()使用python执行的Ruby脚本,python,python-2.7,Python,Python 2.7,我在编写由os.system()执行的ruby脚本的输出时遇到了一个问题 当我尝试在不传递'>out1.txt'的情况下执行代码时,它会被执行并在cmd上显示输出,但当我传递'>out1.txt'参数时,它不会在out1.txt中写入输出。我希望ruby脚本的输出被重定向到txt文件。我会这样做: from subprocess import check_output build = check_output(['\\build.rb', '-p', 'test']) with open('o

我在编写由os.system()执行的ruby脚本的输出时遇到了一个问题

当我尝试在不传递'>out1.txt'的情况下执行代码时,它会被执行并在cmd上显示输出,但当我传递'>out1.txt'参数时,它不会在out1.txt中写入输出。我希望ruby脚本的输出被重定向到txt文件。

我会这样做:

from subprocess import check_output

build = check_output(['\\build.rb', '-p', 'test'])
with open('out1.txt', 'w') as out1:
    out1.write(build)

release = check_output(['\\newRelease.rb', '-l', 'bat'])
with open('out2.txt', 'w') as out2:
    out2.write(release)

您试图将什么作为参数传递,以及传递到何处(python或ruby)?我们需要更多细节来了解问题所在。但是,为什么需要像这样调用Ruby?为什么不用Ruby本身编写脚本呢?另外,Pythons
os.system
不是.twil,Noufal Ibrahim:我正在尝试运行一个包含ruby脚本的python代码,输出应该重定向到txt以维护输出的结果,并且ruby脚本也不能被python代码替换?twil,努法尔·易卜拉欣:我试图在python脚本中执行ruby脚本,需要将该脚本放在文件中:我尝试了上述解决方案,并得到以下错误:回溯(最近一次调用):文件“test.py”,第61行,在main()文件“test.py”中,第55行,在main fl=func_call()文件“test.py”中,第42行,在func_call test=check_输出中(['..\\newRelease.rb','-l',bat'])文件“C:\Python27\lib\subprocess.py”,第568行,在check\u output process=Popen(stdout=PIPE,*popenargs,**kwargs)文件“C:\Python27\lib\subprocess.py”,第711行,在uu init\uu errread,errwrite)文件“C:\Python27\lib\subprocess.py”,第948行,在u execute\child startupinfo)中,错误继续发生。..错误:[Windows193]%1不是有效的Win32应用程序它看起来像Windows不知道您的Ruby脚本是Ruby脚本,或者前面的
\`不属于那里。我尝试三件事:删除
\`,在列表中的每个Ruby脚本名称前添加
'Ruby'
作为参数(如果完整路径不在
%path%
中,则可能是
ruby
,最后,尝试添加
shell=True
作为
检查输出的参数,使其更像旧的
系统()
呼叫。非常感谢您的帮助。您提供的解决方案帮助了我。我做了以下更改,代码运行良好。build=check_输出([ruby'\\build.rb','-p',test'])再次感谢您。使用open('out1.txt',w')作为out1:out1。write(build)release=check_输出(['ruby','\\newRelease.rb','-l',bat']),其中open('out2.txt','w')作为out2:out2.write(release)
from subprocess import check_output

build = check_output(['\\build.rb', '-p', 'test'])
with open('out1.txt', 'w') as out1:
    out1.write(build)

release = check_output(['\\newRelease.rb', '-l', 'bat'])
with open('out2.txt', 'w') as out2:
    out2.write(release)