Python Forum
Subprocess output in windows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Subprocess output in windows (/thread-12280.html)



Subprocess output in windows - Ohmganesh83 - Aug-17-2018

Hi,

Am using subprocess.call() method to send some arguments to executable (myproject.exe).Its working fine am able to pass the arguments and perform the required operation but i want to write the output as text file which is not happening using below code in windows.i have tried some approaches but still not working.

Approach - 1: (Text file is creating but output is not writing)
process = subprocess.call(r"C:\Users\ohm_seenivasan\test\myproject.exe /getUserDetails")            
print(process)
f = open(r"C:\Users\ohm_seenivasan\Desktop\ConsoleReport.txt",'w')
f.write(process)
f.close()
Approach - 2: (Text file is creating but output is not writing)
#file_object =  open(r"C:\Users\ohm_seenivasan\Desktop\ConsoleReport.txt", "w")
            #subprocess.call(r"C:\Users\ohm_seenivasan\test\myproject.exe /getUserDetails", stdout=file_object)
            #file_object.close()
            #print(file_object.read())



RE: Subprocess output in windows - Axel_Erfurt - Aug-17-2018

try subprocess.check_output


RE: Subprocess output in windows - snippsat - Aug-17-2018

call() is just for calling not for capture output.
call() has been replaced bye run(),
which has a parameter capture_output=True
Example capture output from ping.exe.
import subprocess

out = subprocess.run(['ping', 'google.com'], capture_output=True)
print(out.stdout.decode())
Output:
Pinging google.com [216.58.207.238] with 32 bytes of data: Reply from 216.58.207.238: bytes=32 time=36ms TTL=55 Reply from 216.58.207.238: bytes=32 time=34ms TTL=55 Reply from 216.58.207.238: bytes=32 time=33ms TTL=55 Reply from 216.58.207.238: bytes=32 time=32ms TTL=55 Ping statistics for 216.58.207.238: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 32ms, Maximum = 36ms, Average = 33ms
See also that argumet is given as a list ['ping', 'google.com'],
should not pass in strings,if still do so most also use shell=True.
Can also use check_output() to capture output as mention over.


RE: Subprocess output in windows - Ohmganesh83 - Aug-17-2018

We tried above two approaches but did not works.Please give us some other approaches for windows.

i tried below approaches but no luck..

process = subprocess.run([r"C:\Users\test\source\repos\durden2\UpdateCLI\bin\Debug\dusupdate.exe",
r"/GetLastCheckTime"], capture_output=True,shell=True)
print(process.stdout.decode())


RE: Subprocess output in windows - snippsat - Aug-17-2018

You have to use code tags.
There is no shell=True as you use a list as do in demo code.


RE: Subprocess output in windows - Ohmganesh83 - Aug-20-2018

i tired below two approaches ( with & without shell=True) but its not worlking .
It would be very helpful if i get some working example.

approach 1 : (with shell=True)
process = subprocess.run([r"C:\Users\test\source\repos\durden2\UpdateCLI\bin\Debug\dusupdate.exe",
r"/GetLastCheckTime"], capture_output=True,shell=True)
print(process.stdout.decode())

approach 2 : (without shell=True)
process = subprocess.run([r"C:\Users\test\source\repos\durden2\UpdateCLI\bin\Debug\dusupdate.exe",
r"/GetLastCheckTime"], capture_output=True)
print(process.stdout.decode())

in both of my approach its printing blank text.