This question already has answers here:
How do I write to a Python subprocess' stdin?
(5 answers)
Closed 2 years ago.
I have lots of python scripts to be run and I want to automate them. All of them accept inputs at certain times (3 in this case). I have tried something like this but since echo does not have an EOF it did not work:
os.system("echo 4 | echo 5 | echo 6 | python script.py")
I cannot change the content of script.py and it does not accept arguments to it.
How can I automatically input using a line(s) of python code? Thanks.
Check out Pexpect:
Pexpect is a pure Python module for spawning child applications;
controlling them; and responding to expected patterns in their output.
Pexpect works like Don Libes’ Expect. Pexpect allows your script to
spawn a child application and control it as if a human were typing
commands.
Related
This question already has an answer here:
create unix alias using a python3 script
(1 answer)
Closed 2 years ago.
I want to create an alias' through python program. I intend to use python as a replacement for shell scripting.
What I tried is:
import os
os.system('alias go2dir="cd /i/want/to/goto/this/dir"')
...and it does not work. I know the reason - that system command 'alias...' is getting executed in another shell and not in the current one where this python script is executed. So, that alias is not available to this shell.
What I don't know is - (In general,) how do we execute a command from a python program in the same shell where this python program is being executed. So that (in this case) the alias is available till the shell terminal is open?
The way other applications that want to automate actions in the user's shell work is that they write shell commands to their standard output. Then you can execute them with eval.
makealias.py:
print('alias go2dir="cd /i/want/to/goto/this/dir"')
Then in bash:
eval "$(python makealias.py)"
An example of a standard Unix program that works like this is tset with the -s option.
This question already has answers here:
Execute Commands Sequentially in Python?
(5 answers)
Closed 3 years ago.
I have the following line of commands used in sequence from the command window.
cd path 1 && file.bat && cd path 2 && printfNav path3 > Testing.txt
Is it possible to have this done through python?
You are looking for the os.system() method. You will need to import os into your python program and then using this method you can send commands to the terminal. The method takes in a string and acts as if you typed that into your terminal. For example os.system('pwd') would cause the terminal to print out your current working directory, and so on so forth with other commands.
This question already has answers here:
Running windows shell commands with python
(7 answers)
Closed 4 years ago.
I want to open a cmd and then input data into the command line from python. I plan to call PEST calibration software to open from within python and I want to start by opening a cmd.
I am using Python 2.7 and so subprocess doesn't seem to work. I have tried os.system('cmd') and I can open the prompt but I can't input any data.
import os
os.system('cmd')
You should be able to pass the exact resulting string to os.system(). Ex:
os.system('notepad.exe')
In other words, os.system behaves the same way a console would.
This question already has answers here:
Interacting with program after execution
(3 answers)
Closed 7 years ago.
I want a script to run an then finish on the python shell with all variables and methods:
$ python myprogram.py
...
program output
...
>>>
And with #!/usr/bin/python is posible? so I double-click and it just works?
Sounds like you want Python's i flag. From the help menu:
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
So the full command would be
python -i yourscriptname.py
This question already has answers here:
Find and kill a process in one line using bash and regex
(30 answers)
Closed 9 years ago.
How to kill a running python in shell script when we know the python file name xxx.py?
(it is executed by cmd python xxx.py)
I can use ps aux | grep python to get the pid of it. and then
kill pid to terminate it.
but every time, I have to execute two cmd. Is there a good way to do it?
The pkill utility can look at command lines when sending signals:
pkill -f xxx.py