让 Python 在交互式 Perl 脚本中模拟用户输入 [英] Getting Python to emulate user inputs in an interactive Perl script

查看:32
本文介绍了让 Python 在交互式 Perl 脚本中模拟用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,并且一直在努力解决这个问题......

I'm new to programming and have been wrestling this problem for a while now...

我正在编写一个 Python 脚本来自动化基于交互式命令行的 Perl 脚本,以便它可以在指定目录中的每个文件上运行.我可以用 subprocess.callsubprocess.Popen 启动程序,但是我要么打开程序,它就在 shell 中等待用户输入,或者我无限次打开它.我需要 Perl 脚本为每个文件打开一次,接受用户输入(当然,在每次输入后按下类似于输入"的东西,以及一个包含文件名的命令,它应该是 str(files) 在下面的示例代码中),以 "4" 的用户输入开始 Perl 脚本的计算,然后关闭它.

I'm writing a Python script to automate an interactive command-line based Perl script so that it may run on every file in a specified directory. I can get the program to boot with subprocess.call and subprocess.Popen, but from there I either open the program and it sits there waiting in the shell for a user input, or I open it an infinite amount of times. I need the Perl script to open once per file, accept user inputs (with something similar to "enter" being pressed after each input, of course, and a command including the name of the file, which should be str(files) in the example code below), begin the Perl script's calculations with a user input of "4", and then have it close.

我知道这是可能的,但是我尝试了 1000 种不同的方法都无济于事.有人可以给我一个像我 5 岁一样解释它"关于如何让这个 Python 和 Perl 脚本像用户输入命令一样相互交谈?

I know this is possible, but I've tried 1000 different approaches to no avail. Could someone give me an "explain it like I'm 5" as to how I can get this Python and Perl script talking to each other as if a user was inputting commands?

这是我目前所拥有的:

for files in os.listdir("In_Files"):
    print "Performing calculations on " + str(files) + " ..."
    os.chdir("/home/MTS/Dropbox/dir/In_Filess")
    subprocess.Popen("run_command.command -f a", shell=True) #this boots up the Perl script
    time.sleep(3) #waits for the script to open...
    Popen.communicate(input="1") #user inputs begin here
    Popen.wait(0) #am I trying to set a return value here so that the inputs may proceed once the previous one has been accepted?
    Popen.communicate(input=str(files)) #this is where the name of the file the Perl script is working on is entered
    Popen.wait(0)
    Popen.communicate(input="4")
    Popen.wait(0)

我一直在阅读很多关于标准输入管道的内容,但不太了解它们.它们是用于解决这个问题还是应该使用另一种方法?另外,我已经看过 Pexpect,但似乎没有用.

I keep reading a lot about stdin pipes but don't quite understand them. Are they used for this problem or should another approach be used? Also, I've already looked at Pexpect but that doesn't seem to be working.

非常感谢您的帮助.

推荐答案

要为给定目录中的每个文件运行命令:

To run a command for each file from a given directory:

#!/usr/bin/env python
import os
from subprocess import Popen, PIPE

working_dir = b"/home/MTS/Dropbox/dir/In_Files"
for filename in os.listdir(working_dir):
    print("Performing calculations on {filename!r}...".format(**vars()))
    p = Popen(["run_command.command", "-f", "a"], cwd=working_dir, stdin=PIPE)
    p.communicate(input=b"\n".join([b"1", filename, b"4"]))
    if p.returncode != 0: # non-zero exit status may indicate failure
        raise Error

请参阅@skmrx 的回答,了解您当前代码失败的原因.

See @skmrx's answer for the explanation of why your current code fails.

这篇关于让 Python 在交互式 Perl 脚本中模拟用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆