Gethostbyaddr TypeError: gethostbyaddr() argument 1 must be str, bytes or bytearray, not _io.TextIOWrapper

Hi, new to Python. Just trying to put together a small script to read a file of IP addresses and resolve to name, write back to file. small tool and learning exercise for me.

import subprocess
import socket
tstout = open('testout.txt','w+')
with open("testip.txt", "r") as ins:
    for l in ins:
        out = socket.gethostbyaddr(ins)
        tstout.write (out[0])
tstout.close()
subprocess.Popen([r'c:\Windows\notepad.exe',r'c:\scripts\testout.txt'])

Get traceback “TypeError: gethostbyaddr() argument 1 must be str, bytes or bytearray, not _io.TextIOWrapper”

The file opened for read just has an ip address (v4) in one line, nearest I can read this, the IP address line is not being read as a string variable, is there a way to convert the data as it is being read to str ?

python 3

Thanks in advance

You’re passing the open file to socket.gethostbyaddr, not the line your read from the file. Note that l will include the newline at the end of the line.

Thank you for the comment , got me pointed in the right direction, got some formatting stuff to do for the output, but the basics are working now, thanks again.

import subprocess
import socket

tstout = open('testout.txt', 'w+')
with open("testip.txt", "r") as ins:
    for l in ins:
        out = socket.gethostbyaddr(l.strip())
        tstout.write(out[0])
tstout.close()
subprocess.Popen([r'c:\Windows\notepad.exe', r'c:\scripts\testout.txt'])