I have experience with C++ and Delphi. So I know the general concepts of programming. I tried to run this pyton-script in IDLE
import os
with open("ip_list.txt") as file:
park = file.read()
park = park.splitlines()
print(" {park} n")
# ping for each ip in the file
for ip in park:
response = os.popen(f"ping -c 4 {ip} ").read()
# Pinging each IP address 4 times
#saving some ping output details to output file
if("Request timed out." or "unreachable") in response:
print(response)
f = open("ip_output.txt","a")
f.write(str(ip) + ' link is down'+'n')
f.close()
else:
print(response)
f = open("ip_output.txt","a")
f.write(str(ip) + ' is up '+'n')
f.close()
# print output file to screen
with open("ip_output.txt") as file:
output = file.read()
f.close()
print(output)
with open("ip_output.txt","w") as file:
pass
I get this error-message:
Traceback (most recent call last):
File "C:/Users/dipl-/AppData/Local/Programs/Python/Python312/python-read-ip-textfile-ping-store-results-004.py", line 10, in <module>
response = os.popen(f"ping -c 4 {ip} ").read()
File "C:Usersdipl-AppDataLocalProgramsPythonPython312Libencodingscp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 21: character maps to <undefined>
I did some research on this error-message and found some answers telling that the encoding is wrong and that an encoding must be added.
How do I do add a certain type of encoding?
Do I have to use cp1252-encoding?
And as this script uses a user-written textfile with IPAdresses how the heck do I save such a file with cp1252-enocding?
From these questions you can see: zero knowledge about how to configure encoding or how to modify this script to work with “standard” textfiles written in Notepadd++ or something similar
I tried to add encoding with this modification of the code
import os
with open("ip_list.txt",encoding="latin1") as file:
park = file.read()
park = park.splitlines()
print(" {park} n")
# ping for each ip in the file
for ip in park:
response = os.popen(f"ping -c 4 {ip} ").read()
# Pinging each IP address 4 times
#saving some ping output details to output file
if("Request timed out." or "unreachable") in response:
print(response)
f = open("ip_output.txt","a",encoding="latin1")
f.write(str(ip) + ' link is down'+'n')
f.close()
else:
print(response)
f = open("ip_output.txt","a",encoding="latin1")
f.write(str(ip) + ' is up '+'n')
f.close()
# print output file to screen
with open("ip_output.txt",encoding="latin1") as file:
output = file.read()
f.close()
print(output)
with open("ip_output.txt","w",encoding="latin1") as file:
pass
Error stays the same
error-message:
Traceback (most recent call last):
File "C:Usersdipl-AppDataLocalProgramsPythonPython312python-read-ip-textfile-ping-store-results-003.py", line 10, in <module>
response = os.popen(f"ping -c 4 {ip} ").read()
File "C:Usersdipl-AppDataLocalProgramsPythonPython312Libencodingscp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 21: character maps to <undefined>
What do I have to change in the code to get rid of this encoding-error?
Billy Gates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Based on the traceback, the encoding error doesn’t come from file handling at all, but the ping
command you’re running.
I’d recommend using subprocess.getoutput()
instead, so you can control what happens with decoding errors; here, we’ll just replace them with �:
response = subprocess.getoutput(f"ping -c 4 {ip}", errors="replace")
On a more general note, it’s worth using Python’s UTF-8 Mode on Windows to avoid various charmap
and cp1252
shenanigans.