I want to automatize upgrade of a program.
I run in Python this code:
import subprocess
subprocess.call('./upgrade')
When I do this, I get output from shell that Upgrade procedure started successfully, and then I get 'Press Enter to continue'. How would I automatize this process, so that python script automatically "presses" enter when promted? I need this to be done twice during procedure.
I need this to be done on Linux, not Windows, as it was asked here:
Generate keyboard events
Also, this needs to be done specifically after Shell prompts for Enter.
Thanks for any help.
I did not find solution here:
Press enter as command input
You can use subprocess.Popen and subprocess.communicate to send input to another program.
For example, to send "enter" key to the following program test_enter.py:
print "press enter..."
raw_input()
print "yay!"
You can do the following:
from subprocess import Popen, PIPE
p = Popen(['python test_enter.py'], stdin=PIPE, shell=True)
p.communicate(input='\n')
You may find answer to "how do i write to a python subprocess' stdin" helpful as well.
Related
This code,
https://i.stack.imgur.com/E39QL.png
It opens the shell but as I fill the input fields, It closes without showing the output after creating its exe file.
main.py file,
https://i.stack.imgur.com/fKKTq.png
What can I do so that the shell remains visible after the code is completed?
The answer of a * b is shown so quickly that I can't even see the output and the shell closes.
Your problem is that the Python code exit with code 0 correctly and, consequently, the shell closes.
A solution could be adding a "press any key to exit" command at the end of your code. To do that you can use readchar as below. You can put it at the end of your code so when you click something, the shell closes. You also need to isntall it via pip install readchar
import readchar
print("Press Any Key To Exit")
k = readchar.readchar()
I am trying to write the codes to run a C executable using Python.
The C program can be run in the terminal just by calling ./myprogram and it will prompt a selection menu, as shown below:
1. Login
2. Register
Now, using Python and subprocess, I write the following codes:
import subprocess
subprocess.run(["./myprogram"])
The Python program runs but it shows nothing (No errors too!). Any ideas why it is happening?
When I tried:
import subprocess
subprocess.run(["ls"])
All the files in that particular directory are showing. So I assume this is right.
You have to open the subprocess like this:
import subprocess
cmd = subprocess.Popen(['./myprogram'], stdin=subprocess.PIPE)
This means that cmd will have a .stdin you can write to; print by default sends output to your Python script's stdout, which has no connection with the subprocess' stdin. So do that:
cmd.stdin.write('1\n') # tell myprogram to select 1
and then quite probably you should:
cmd.stdin.flush() # don't let your input stay in in-memory-buffers
or
cmd.stdin.close() # if you're done with writing to the subprocess.
PS If your Python script is a long-running process on a *nix system and you notice your subprocess has ended but is still displayed as a Z (zombie) process, please check that answer.
Maybe flush stdout?
print("", flush=True,end="")
I'm calling subprocess.run to execute an external program outside. However the program requires administrative rights, I run the program using administrator but the python console prompts me for a password, but doesn't let me input the password and exits the program.
I've tried using subprocess.popen and subprocess.call, I've also tried running the program without administrative rights but pycharm will throw me a operation requires elevation error.
def runExecutables():
directory = r"C:\Users\Billy\Desktop\SURVEY_PROGRAM_WINDOWS_ENGLISH.exe"
#subprocess.Popen(directory)
subprocess.run(['runas', '/user:Administrator', directory])
#prog.stdin.write(b'password')
#prog.communicate()
I should be expecting, either the executable to run, or a prompt that pops up asking for the password to be inputted, proving to me that the executable is indeed being run. I am just getting a python prompt to enter the pass for administrator and it does not wait for me to enter the password before finishing the process.
With Popen, you have to pipe in stdin and flush the input
import subprocess
p = subprocess.Popen(['runas', '/user:Administrator', 'calc.exe'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write(b'YourPassword\n')
p.stdin.flush()
stdout, stderr = p.communicate()
print("-----------")
print(stdout)
print("-----------")
print(stderr)
I have avoided this problem by approaching it using command prompt rather than using subprocess.
def runExecutables():
os.system(r"start C:\Users\Mastodon\Desktop\SURVEY_PROGRAM_WINDOWS_ENGLISH.exe")
Using command prompt alleviates some of the problems that subprocess would inflict. I am unclear as to what the advantages of using subprocess are.
I need to automate a few bash scripts which involves answering to read prompts with y/n.
I tried to pipe stdout/stderr/stdin to a python script. Writing to stdin works but reading the prompt text from stdout/stderr doesn't for some reason? (I can read everything else that bash or sub-processes output fine.)
>>> from subprocess import Popen, PIPE
>>> proc = Popen(['bash','-c','read -r -p "Update system? [y/N] " response'],stdout=PIPE,stdin=PIPE,stderr=PIPE)
>>> proc.stdout.read(10) # <-- hangs, same with stderr, any length
I was expecting I would be able to read displayed prompt "Update system? [y/N] " somehow so I can decide what answer to pass back.
This is what expect is good at:
https://likegeeks.com/expect-command/
Expect and bash
https://unix.stackexchange.com/questions/351446/bash-and-expect-in-the-same-script
I am attempting to to launch a python script from within another python script, but in a minimized console, then return control to the original shell.
I am able to open the required script in a new shell below, but it's not minimized:
#!/usr/bin/env python
import os
import sys
import subprocess
pyTivoPath="c:\pyTivo\pyTivo.py"
print "Testing: Open New Console"
subprocess.Popen([sys.executable, pyTivoPath], creationflags = subprocess.CREATE_NEW_CONSOLE)
print
raw_input("Press Enter to continue...")
Further, I will need to be able to later remotely KILL this shell from the original script, so I suspect I'll need to be explicit in naming the new process. Correct?
Looking for pointers, please. Thanks!
Note: python27 is mandatory for this application. Eventually will also need to work on Mac and Linux.
Do you need to have the other console open? If you now the commands to be sent, then I'd recommend using Popen.communicate(input="Shell commands") and it will automate the process for you.
So you could write something along the lines of:
# Commands to pass into subprocess (each command is separated by a newline)
commands = (
"command1\n" +
"command2\n"
)
# Your process
py_process = subprocess.Popen(*yourprocess_here*, stdin=PIPE, shell=True)
# Feed process the needed input
py_process.communicate(input=commands)
# Terminate when finished
py_process.terminate()
The code above will execute the process you specify and even send commands but it won't open a new console.