如何创建一个脚本来执行Python中的执行管理器?
问题描述:
我试图把这种PERL代码到Python的:到目前为止如何创建一个脚本来执行Python中的执行管理器?
# Create a script on the fly to execute w/ the execution manager
unlink "logger_exit_test.pl";
open my $fh, '>', "logger_exit_test.pl" or die "Unable to create
logger_exit_test.pl";
print {$fh} <<EOF;
#!$EXECUTABLE_NAME
ISC::message(\$ARGV[0], MESSAGE => "test");
EOF
close $fh;
chmod 0750, "logger_exit_test.pl";
,我有这样的Python代码:
## Create a script on the fly to execute w/ the execution manager
try:
os.remove("logger_exit_test.py")
except OSError:
pass
open("logger_exit_test.py", "w+")
with open('logger_exit_test.py') as fh:
for line in fh:
print line
if 'str' in line:
break
executable_name = sys.executable()
ISC.message(sys.argv[0], MESSAGE("test"))
f.close()
os.chmod("logger_exit_test.py", stat.S_IRWXU)
到目前为止,我一直不成功创建可执行...失败:
executable_name = sys.executable()
答
这是用于打开文件的写( “W +”)perl的成语,
open my $fh, '>', "logger_exit_test.pl" or die "Unable to create logger_exit_test.pl";
这些行在perl中构造文件(什么是#的名字! (家当)程序来运行这个文件?$ EXECUTABLE_NAME),
print {$fh} <<EOF;
#!$EXECUTABLE_NAME
ISC::message(\$ARGV[0], MESSAGE => "test");
EOF
close $fh;
你似乎会问蟒蛇来确定路径到Python,但你打算使用Python解释器,或Perl解释器来执行你构建的文件(你使用.py扩展名,所以我的猜测是你想使用python)?
executable_name = sys.executable
这是打开文件编写的Python IDOM,你将需要导入Python库(IES)适当,
with open("logger_exit_test.py", "w+") as fh:
fh.write("#!${}\n".format(executable_name))
fh.write("#import appropriate_package as ISC\n")
fh.write("""ISC.message(sys.argv[0], MESSAGE("test");\n""")
#since you used with open(), close not needed,
os.chmod("logger_exit_test.py", stat.S_IRWXU)
在这里寻找如何执行上述文件, run child process from python
谢谢,查克!这确实有帮助。 :) –