AlexW AlexW - 1 year ago 125
Python Question

Python - ValueError: I/O operation on closed file - traceroute script + printing

ive seen this error mentioned a few times on here when i look up the questiotns but most seem to be related to opening and closing files.

I copied this traceroute script and ive added an argparse to it. i also wanted to add a print to it as im running it as a cron and logging to a file. i wanted a time stamp in the log file. Thus i added the print at the bottom and now get an error


import socket
import struct
import sys
import argparse
import datetime

# We want unbuffered stdout so we can provide live feedback for
# each TTL. You could also use the "-u" flag to Python.
class flushfile(file):
def __init__(self, f):
self.f = f
def write(self, x):

sys.stdout = flushfile(sys.stdout)

def main(dest):
dest_addr = socket.gethostbyname(dest)
port = 33434
max_hops = 30
icmp = socket.getprotobyname('icmp')
udp = socket.getprotobyname('udp')
ttl = 1
while True:
recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)

# Build the GNU timeval struct (seconds, microseconds)
timeout = struct.pack("ll", 5, 0)

# Set the receive timeout so we behave more like regular traceroute
recv_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeout)

recv_socket.bind(("", port))
sys.stdout.write(" %d " % ttl)
send_socket.sendto("", (dest, port))
curr_addr = None
curr_name = None
finished = False
tries = 3
while not finished and tries > 0:
_, curr_addr = recv_socket.recvfrom(512)
finished = True
curr_addr = curr_addr[0]
curr_name = socket.gethostbyaddr(curr_addr)[0]
except socket.error:
curr_name = curr_addr
except socket.error as (errno, errmsg):
tries = tries - 1
sys.stdout.write("* ")


if not finished:

if curr_addr is not None:
curr_host = "%s (%s)" % (curr_name, curr_addr)
curr_host = ""
sys.stdout.write("%s\n" % (curr_host))

ttl += 1
if curr_addr == dest_addr or ttl > max_hops:

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Traceroute')
parser.add_argument('-H' '--dest',
help='IP or Hostname',
parser_args = parser.parse_args()
print 'Trace completed at:{0}'.format('%d-%m-%Y %H:%M:%S'))


Traceback (most recent call last):
File "", line 83, in <module>
print 'Trace completed at:{0}'.format('%d-%m-%Y %H:%M:%S'))
ValueError: I/O operation on closed file

Answer Source

It looks like the issue is that near the top of the script sys.stdout is reassigned to a custom class which only supports the write() operation. Print is probably trying to call a method on sys.stdout but it no longer has that method.

An easy fix would be to use sys.stdout.write instead of print at the end of the script.

For example:

        'Trace completed {0}\n'.
        strftime('%d-%m-%Y %H:%M:%S'))

Another option would be to save the original value of sys.stdout and restore it near the end of the script, before the print:

old_sys_stdout = sys.stdout
sys.stdout = flushfile(sys.stdout)
sys.stdout = old_sys_stdout
#print as normal
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download