I'm new to programming and have been wrestling this problem for a while now...
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.
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?
Here's what I have so far:
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)
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.
Your help is immensely appreciated.