Printing Popen Output Using Windows 7

Hi Guys,

I am new to python and I am trying to print ouput of Popen on my text screen (tkinter gui).

I was able to make it work on Linux with this code:

Linux: Working


def PrintSomething2():
	outputdata = commands.getstatusoutput("sudo fping -f host.list")
	for i in outputdata:
		print i
	sp.call(["rm", "host.list"])

However, I am trying to do the same for a windows machine but it is not working:

Windows: NOT Working


import subprocess as sp
from subprocess import Popen
import sys
import commands

def PrintSomething2():
	outputdata = Popen("scanping.bat")
	for i in outputdata:
		print i
	deletefile = Popen("DEL", "sites.txt")

Note: It is printing in my terminal though:

Let me know if you have any tips. Thanks in advanced!

deletefile = Popen(["del", "sites.txt"], shell=True, stdout=subprocess.PIPE)
1 Like

This one worked in deleting the file after the script executes! Thank you!

However, I am still not getting the ouput in the CLI to print in my screen.

Do you have a good method of printing the output of:


def PrintSomething2():
    outputdata = Popen("scanping.bat", cwd=r"C:\Users\jbantay\OneDrive\Python")
    print outputdata
    deletefile = Popen(["del", "sites.txt"], shell=True, stdout=sp.PIPE)

Because the error I am getting is:

Please take note that this code is working when I am using a linux machine:


def PrintSomething2():
	outputdata = commands.getstatusoutput("sudo fping -f host.list")
	for i in outputdata:
		print i
	sp.call(["rm", "host.list"])

---------- Post updated at 03:52 AM ---------- Previous update was at 03:46 AM ----------

This is the entire code of it:

https://docs.google.com/document/d/1vpOiFPivIJr2gSpVBMY2tLDn2jYz-TnL1Ch8R0ySbZI/edit

Thanks!

---------- Post updated at 04:13 AM ---------- Previous update was at 03:52 AM ----------

Solved it by using code below! :slight_smile:


def PrintSomething2():
    outputdata = sp.Popen(["scanping.bat"], stdout=sp.PIPE).communicate()[0]
    print outputdata
    deletefile = Popen(["del", "sites.txt"], shell=True, stdout=sp.PIPE)

---------- Post updated at 04:15 AM ---------- Previous update was at 04:13 AM ----------

Here is the output printing in Tkinter: