I have a program (let's say program.exe) that can be executed on a command line. After being executed, it asks for some inputs, runs for a while, and gives some outputs. I need to write a python script that interacts with this program so that it can continuously send/receive inputs/outputs.
I have tried the libraries subprocess and Pexpect, but so far I have not managed to implement this functionality. If anyone knows how to do this I would appreciate a working example so I can adapt it.
Thank you very much for your help!
Receive inputs:
to grab some returned values from shell scripts you can use the argument standard input like argv and argc
to invoke cmd command:
Use the system.os and usit as os("<your shell command>")
Related
I'm trying to create a bash script that can write values into a separate python shell that is constantly running, which is expecting user input, then read back the returned value into my bash script.
Backstory: I can't just call the python script from directly within the bash script as the python script is constantly running, with a fairly long initialization which I don't want to do each time I call it.
So it can be condensed into two questions;
How can I write to python 'user input' programmatically within bash
How can I reliably read what is returned from the python script into a bash variable (it can be printed out and returned however I like)
I am familiar with bash basics, but I am still fairly new to it. I am guessing it has something to do with reading/writing to stderr, stdout, or stdin?
Any advice given would be very useful, thanks.
After further searching I found that sockets are definitely the way to go. The example I found here worked perfectly:
Basic Python client socket example
Thank you Shardj for the suggestion
Firstly, I have very limited experience with programming. So please bear with me.
I am using a custom software, called deformcyto, made for my lab which can only be started via anaconda prompt. I need to call the same program multiple times but with different inputs. Can I write a python script which would keep calling the program with new arguments?
Thanks for any pointers!
You can try something like this, if your program is an executable.
os.system(r"C:\youdir\..\yourprogram.exe")
You can also write a bash script or a powershell script and start the program multiple times with different arguments.
Well I have tried all of methods available on StackOverflow
1) !myfile.exe args
2) subprocess.call() and 3) subprocess.POpen() as per What's the difference between subprocess Popen and call (how can I use them)?
4) os.system as per How to run external executable using Python?
My program starts, but I am not seeing any output window, Actually my.exe programme is compiled using VisualStudio and I am taking few inputs in that .exe program using StdIn,
I have tried os.wait(), subprocess.wait()`` os.sleep(3) command my still output comes and disappears.
I have also tried this how to run an exe file with the arguments using python but problem is same.
What does my .exe program do?
Actually I am giving one file ".bin" file as command argument to my program. Further my program prompts user to take few strings as input using Std In.
If you run your python program from the CMD you can insert a time.sleep() in the python code, which will make the output stay a bit longer.
Otherwise, in order to be in line with your question a bit more, try making an external temp script that will run your .exe with a pause command in it or something.
I have the situation that I want the job done from eclipse so I use eclipse's .launch configuration. I tried to make it run python directly but got error: error 193 (%1 is not a valid Win32 app). where %1 is probably my python script.
I decided to create a simple batch script that calls this big wild python animal.
I did a lot of combinations and found this the best (batch outputs some strings, runs python, waits for it, the outputs some strings again):
start /b /wait "Python_script.py" "%1" "%2" "%3" "%4" "%5"
It worked until python itself started to run exe file.
Once again I tried a lot of combinations:
os.system([exe, arg1, arg2, ...]) and
subprocess.call(..) and subprocess.check_output(..)
-> I either didn't see the output in eclipse console, or the output was delayed or there was only python / or only exe's output in console.
finally I used subprocess.Popen(...) and it's nearly allright - the only defect is that the output from python script don't wait for exe's process to finish, and when I use subprocess.Popen(...).wait() exe passes output to console but the WHOLE output from python script is delayed until the 'exe' terminates. I want to delay only the part of pythons script output that is written after the exe is called.
how to achieve this 'partly console output delay' is the main topic
advices on python and eclipse .launch configuration will be appreciated
general advices on how does the communication between this processes(?) work will be appreciated
Thanks!
It sounds to me like you have three different processes you're trying to get to work together, you've tried a whole bunch of stuff to get it working, and the code is complex enough that you can't easily post it here. That makes it pretty hard to get a good answer (Stack Overflow works much better with focused questions), but here's the general approach I'd take:
Does your script run if you try to run Python_script.py directly from the command prompt?
If it doesn't, then look into registering the .py file type in Windows.
If it does, then maybe Eclipse launch configurations don't support or don't properly support Windows registered file types. There should be no need to mess with batch files and start; just replace Python_script.py in your launch configuration with c:\Python27\python.exe Python_script.py (or similar).
Get your script working from a command prompt - able to run, with proper Python and subprocess output, and waiting for everything to terminate.
If things work from the command prompt and still don't work from Eclipse, then post a new question with a small snippet of code showing what you're trying and a description of what's wrong. subprocess.call, subprocess.check_output, and Popen all have different uses, so it's hard to give general advice besides just referring to the documentation.
I would like to create a simple Python program that will concurrently execute 2 independent scripts. For now, the two scripts just print a sequence of numbers but my intention is to use this program to concurrently run a few Twitter streaming programs in the future.
I suspect I need to use subprocess.Popen but I cannot quite get my head around what arguments I should put in there. There was a similar question on StackOverflow but the code provided there (pasted below) doesn't print anything. I will appreciate your help.
My files are:
thread1.py
thread2.py
import subprocess
subprocess.Popen(['screen', './thread1.py']))
subprocess.Popen(['screen', './thread2.py'])
Use supervisord
supervisord is process control system just for the purpose of running multiple command line scripts.
It features:
multiple controlled processes
autorestarting failed runs
log stdout and stderr output
starting scripts in order (using priority)
command line utility to view latest log output, stop, start, restart the processes
This solution works only on *nix based systems, it is not available on Windows.
As wanderlust mentioned, why do you want to do it this way and not via linux command line?
Otherwise, the solution you post is doing what it is meant to, i.e, you are doing this at the command line:
screen ./thread1.py
screen ./thread2.py
This will open a screen session and run the program and output within this screen session, such that you will not see the output on your terminal directly. To trouble shoot your output, just execute the scripts without the screen call:
import subprocess
subprocess.Popen(['./thread1.py'])
subprocess.Popen(['./thread2.py'])
Content of thread1.py:
#!/usr/bin/env python
def countToTen():
for i in range(10):
print i
countToTen()
Content of thread2.py:
#!/usr/bin/env python
def countToHundreds():
for i in range(10):
print i*100
countToHundreds()
Then don't forget to do this on the command line:
chmod u+x thread*.py
You can also just open several Command Prompt windows to run several Python programs at once - just run one in each of them:
In each Command Prompt window, go to the correct directory (such as C:/Python27) and then type 'python YourCodeNo1.py' in one Command Prompt window, 'python YourCodeNo2.py' in the next one ect. .
I'm currently running 3 codes at one time in this way, without slowing any of them down.