Run Perl code (with output to file) from Python

后端 未结 1 1394
滥情空心
滥情空心 2021-01-07 01:42

I\'m trying to run a Perl script from Python. I know that if run the Perl script in terminal and I want the output of the Perl script to be written a file I need to add

相关标签:
1条回答
  • 2021-01-07 02:11
    subprocess.call("perl myCode.pl >results.txt", shell=True)
    

    or

    subprocess.call(["sh", "-c", "perl myCode.pl >results.txt"])
    

    or

    with open('results.txt', 'wb', 0) as file:
        subprocess.call(["perl", "myCode.pl"], stdout=file)
    

    The first two invoke a shell to execute the shell command perl myCode.pl > results.txt. The last one executes perl directly by having call do the redirection itself. This is the more reliable solution.

    0 讨论(0)
提交回复
热议问题