简体   繁体   English

运行Perl脚本并从Python输入命令

[英]Running a Perl script and entering commands into it from Python

I'm a week into learning Python and am trying to write a piece of code that allows me to run a text-based Perl script in LXTerminal automatically. 我学习Python已有一周的时间,并且正在尝试编写一段代码,使我可以在LXTerminal中自动运行基于文本的Perl脚本。 I have a couple of questions regarding some specifics. 关于一些细节,我有几个问题。

I need my code to start the Perl script with a user-inputted environment file, enter a few specific settings into the Perl script, and then read in many .txt files, one at a time, into the Perl script. 我需要我的代码来使用用户输入的环境文件启动Perl脚本,在Perl脚本中输入一些特定设置,然后一次读取许多.txt文件到Perl脚本中。 It also needs to restart the process for every single .txt file and capture each individual output (it would help if every output could be written to a single .csv file). 它还需要针对每个.txt文件重新启动该过程,并捕获每个单独的输出(如果每个输出都可以写入单个.csv文件,这将有所帮助)。

To call the Perl script, I'm starting with the following: 要调用Perl脚本,我从以下内容开始:

alphamelts="/home/melts/Desktop/alphamelts"
pipe=subprocess.Popen(["perl", "/home/Desktop/melts/alphaMELTS", "run_alphamelts.command -f %s"]) % raw_input("Enter an environment  file:"), stdout=PIPE

Assuming that's correct, I now need it to read in a .txt file, enter number-based commands, have my code wait for the Perl script to finish its calculations, and I need it to write the output to a .csv file. 假设这是正确的,现在我需要它读取.txt文件,输入基于数字的命令,让我的代码等待Perl脚本完成其计算,并且需要它将输出写入.csv文件。 If it helps, the Perl script I'm running automatically generates a space delimited file containing the results of its calculations once the program exists, but it would be super helpful if only a few of its outputs were written onto a single seperate .csv file for each .txt file processed. 如果有帮助,一旦程序存在,我正在运行的Perl脚本会自动生成一个空格分隔的文件,其中包含其计算结果,但是如果仅将其输出的一部分写到单个单独的.csv文件中,它将非常有帮助对于每个处理的.txt文件。

No idea where to go from here but I absolutely have to get this working. 不知道从这里去哪里,但我绝对必须使这个工作正常。 Sorry for the complexity. 抱歉,太复杂了。

Thank you! 谢谢!

you can do some really cool stuff in ipython. 您可以在ipython中做一些非常酷的事情。 Check out this notebook for some specific examples. 查看此笔记本了解一些具体示例。 As far as waiting for a subprocess to finish, I think you need to put a pause in your script. 至于等待子进程完成,我认为您需要在脚本中pause一下。 Also, for data handling and export to csv and excel, I'd recommend pandas Just something to get you started. 另外,对于数据处理以及导出到csv和excel,我建议您使用pandas入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM