convert os.system to subprocess.Popen - python-2.7

I am writing a script to open a new command prompt, and run an executable. I got a working code using os.system. Can you help me transform it to subprocess.Popen?
hello_exe = r'c:\hello.exe'
os.system("start cmd /k {}".format(hello_exe))
I cannot run hello_exe as a background process because I want the user to see the command prompt and be able to scroll through the logs. Any help is appreciated. Thanks.
My environment: Windows8 and python 2.7.12

Conversion to subprocess.Popen is like this:
import subprocess
process = subprocess.Popen(r'c:\hello.exe', shell = True)
process.communicate()

Conversion to Popen
from subprocess import Popen, PIPE
hello_exe = r'c:\hello.exe'
proc = Popen(hello_exe, stdout=PIPE, shell = True)
proc.communicate()
Here are few links which you can refer to understand subprocess.Popen
Link to understand Popen
Another link to understand Popen
Link to 50+ examples of Popen

Related

Run source command using python and keep the environment intact for next set of commands

I am trying to use subproces.Popen() to launch some command like below :
cmd = source /dev/stdin <<< "$(docker run --rm XX/YY-application:${APP_TAG} test)"
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, executable="/bin/bash"
)
Now it doesn't return anything, and even using proc.stdout.readline() returns me new line character. While running the same cmd, on the shell gives me proper output.
So, I have two issues:
1. why printing the stdout not giving me anything?
2. how can I save the environment created by cmd, to be used in subsequent command?

No stdout when attempting to run a python file with subprocess.run()

I'm trying to run a python file from within my python script. I'm doubtful that the file is even getting run in the first place and it is not showing anything in the stdout either for me to debug.
I have tried the command 'ls' in subprocess and it worked, and was in the proper directory that the temp.py file i am trying to run is.
When i have tried to set the argument 'shell=True' it takes me into the python repl for some reason i am not sure why.
Here is the string output:
Terminal output: CompletedProcess(args=['python3', 'temp.py'], returncode=0, stdout=b'')
And here's the code used to produce it:
result = subprocess.run(['python3', 'temp.py'], stdout=subprocess.PIPE, check=True)
print('Terminal output: '+str(result))
EDIT
I also just tried
process = Popen(['python3', 'temp.py'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print('Terminal output: '+str(stdout)+str(stderr))
No cigar:
Terminal output: b''b''
So, I found an alternative that worked. I dumped all the contents of the file into the python3 command with the '-c' augment.
process = Popen(['python3','-u', '-c', strCode], stdout=PIPE, stderr=PIPE)
strCode being the contents of the file.

How can i execute tcl command [using tclsh prompt] remotely using python 2.7 module?

Here is my set up.
Windows PC1 where Python 2.7 code is running ----> Windows PC2 where tcl is installed.
Windows PC1 and Windows PC2 are connected through LAN and able to access each other.
Now i want to open CMD line (As administrator) of PC2 from PC1 using python and execute windows command.
Yes this can be achieved through paramiko.
Now i want to run "tclsh" command in PC2 command prompt. After this the command line goes inside tcl prompt [%].Here i can execute tcl command like puts "Welcome". So if i try to execute "tclsh" command through paramiko its is in waiting state as the prompt has been changed from windows prompt to %.
My intention is to run tcl command in tclsh prompt [PC2 cmd line] through python [PC1].
Example: From PC1 i want to execute "puts "Hello" to PC2 tcl prompt through python2.7.
Please suggest me some idea what can be used [python module or any other approach] to access tclsh prompt through python 2.7.
Through paramiko normal windows commands are working as expected but tclsh enter into % prompt so paramiko is not working in this scenario.
I want to get access to tcl prompt through python 2.7 remotely.
I want to execute tcl command inside tclsh prompt in remote system through python.Here is the small program i have tried .
host = '192.168.1.4'
user = 'lenovo'
passw = 'XXX'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, password=passw)
stdin, stdout, stderr = ssh.exec_command('tclsh')
print (str('.'.join(stdout.readlines())))
print (str('.'.join(stdin.readlines())))
print (str('.'.join(stderr.readlines())))
But i do not see any output as tclsh goes into new % prompt. How do i solve this? After i have access to tclsh % prompt i want to execute "puts "Welcome""
.Instead of tclsh if give "hostname" windows command i get reply.
I got the solutions to access tclsh prompt for a remote windows pc using pramiko in python 2.7.
Step1: Use invoke shell method for tclsh command for the first time and store the return of invoke_shell to a globa variable.Suppose the return is x.
Step2: Now for any other command for tclsh prompt [like cmd = puts "stack"] use like x.send(cmd+"\n")
Step3: Now we can get output buffer like x.recv(9999)
Note [For windows only]: x.recv(9999) returns actual output with many escape sequences. We need to clean that to get the actual data.

QProcess can launch programs but not python (command line)

The following code is failing to launch the python command line.
QProcess *myProcess = new QProcess(this);
myProcess->start("\"C:\\Program Files\\Python27\\python.exe\"");
If I replace python27 with (for example)
myProcess->start("\"C:\\Program Files\\Notepad++\\notepad++.exe\"")
notepad opens. Why is my program able to launch notepad but not Python Command Line?
I tried using startDetached() as suggested here but that didn't make a difference.
QProcess::Error() gives me error 5: unknown error.
If you just want to use the 'python console' you must use cmd.exe application from windows
You must have python in PATH so the windows console will know where to take it from.
So, you can try: QProcess::startDetached("cmd", "python")..see more specific syntax details here
It seems I've misunderstood what happens when you launch a command line. I was expecting the python command line or command prompt window to open.
It turns out that if I just pass my commands as arguments start() like so:
myProcess->start("cmd.exe /C python C:\\Users\\SP4\\Desktop\\helloworld.py");
Command prompt runs my python script and I get the output ("Hello World") using:
QString output = myProcess->readAllStandardOutput();
All this happens in the background and you can't actually see a command line window open and print out "Hello, World".
Please correct me if I've misunderstood something.

command prompt appears than immediately disappears

I encountered this problem after following a python tutorial on Youtube about creating text files. The instructor had us type in the following code to start:
def createFile(dest):
print dest
if__name__ == '__main__':
createFile('ham')
raw_input('done!')
We had created a folder on the desktop to store the file 'ham' in. But when I double clicked on 'ham' the command prompt window popped on then in a flash it popped off. I am an obvious beginner and I don't know what is going on. Can anyone explain this to me?
You can open command prompt then navigate to python interpreter
directory and run your program by typing python /diretory/to/your/program.py for
example if you have a program named test.py in the directory c:/python and you want to
run it and you have python interpreter installed in C:/python2.x/ directory
then you should open command prompt and type in
cd c:\python2.x\
then you should type
python c:/python/test.py
and perfectly it will work
showing you your program output .