2017-04-12 76 views
1

我試圖把這種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()

回答

0

這是用於打開文件的寫( 「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

+0

謝謝,查克!這確實有幫助。 :) –