The problem is that you're trying to print out a Python 3 bytes
object, which Python cannot automatically convert to a str
object, because it can't be certain what the character encoding is.
You'll have to convert it to a string, telling Python what the encoding is, using the bytes
object's decode()
method...
import subprocess
hosts_file = open("hosts.txt","r")
lines = hosts_file.readlines()
for line in lines:
line = line.strip()
ping = subprocess.Popen(["ping", "-n", "3",line],stdout = subprocess.PIPE,stderr = subprocess.PIPE)
out, error = ping.communicate()
out = out.strip()
error = error.strip()
output = open("PingResults.txt",'a')
output.write(str(out))
output.write(str(error))
print(out.decode('utf-8'))
print(error.decode('utf-8'))
hosts_file.close()