Python Forum
Subprocess output in windows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subprocess output in windows
#1
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())
Reply
#2
try subprocess.check_output
Reply
#3
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.
Reply
#4
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())
Reply
#5
You have to use code tags.
There is no shell=True as you use a list as do in demo code.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  merge two csv files into output.csv using Subprocess mg24 9 1,692 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  How to get program output from subprocess.Popen? glestwid 1 2,334 Aug-19-2020, 05:44 AM
Last Post: buran
  How to use subprocess send commands to windows shell hlhp 3 4,366 Nov-26-2019, 04:40 AM
Last Post: LeanbridgeTech
  A question about subprocess taking input from command line and returning output! Aurimas 8 5,107 May-15-2019, 04:02 PM
Last Post: Aurimas
  Subprocess command prompt (Windows) arnaur 6 10,196 Sep-06-2018, 07:22 AM
Last Post: arnaur
  [inconsistent output] subprocess.call to run cmd commands to get Kerberos ticket Yelin 2 4,944 Jun-08-2018, 09:02 AM
Last Post: Yelin
  Python on Windows 2012RC2 (with VS Code 1.22.2). Scripts not generating output. JGFMK 2 3,166 Apr-24-2018, 08:17 PM
Last Post: JGFMK
  Change Windows Sound Output Device with Python delfar 1 10,231 Sep-15-2017, 12:11 AM
Last Post: Larz60+
  [subprocess]>Run a cmd command and get output into a variable CSA75 4 22,710 Mar-13-2017, 09:33 PM
Last Post: Ofnuts
  Windows: python script call over ssh -> output jb7731 2 5,296 Feb-08-2017, 01:13 PM
Last Post: jb7731

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020