0

I am running a python script on a screen on linux server and when I do TOP command I can see it running but it has been for hours that the script did not write anything. Anybody know what could be the reason?

Here is my script:

import GeoIP
from netaddr import *

gi = GeoIP.GeoIP("/data/GeoIPOrg_20141202.dat", GeoIP.GEOIP_MEMORY_CACHE)
o = Path to the output text file

for line in f:
    line = line.strip('\n')
    asn,ip,count =line.split('|')
    org = gi.org_by_addr(ip)
    start,end = gi.range_by_ip(ip)
    ran = list(IPRange(start,end))
#    ipcount = len(ran)
    ip_start,ip_end = IPAddress(start),IPAddress(end)
    n_start = int(ip_start)
    n_end = int(ip_end)
    range = (n_start,n_end)
    print ("%s|%s|%s|%s|%s|%s" % (asn,range,len(ran),org,ip,count) , file = o)
UserYmY
  • 5,838
  • 13
  • 45
  • 67

2 Answers2

1

This could be a few things; hard to say without seeing how you're running and how you're initialising that file.

A definite possibility is the file isn't being flushed (more relevantly, see the docs on changing the buffer size of open() as it's probably being invoked in your code).

Either way it's worth using Python (2.5+)'s with statement to handle file / resource management neatly and robustly instead of relying on print e.g.:

with open("/my/output/path.txt", "w") as out_file:
    # Rest of code
    # ...
    out_file.write("%s|%s|%s|%s|%s|%s\n" % (asn,range,len(ran),org,ip,count))

See this SO question for good examples of using the with statement.

Community
  • 1
  • 1
declension
  • 3,764
  • 19
  • 23
1

You have two ways to achieve this.

  1. You can change your code to use the with statement (context manager) as @Nick B has suggested in his answer to open your file there.
  2. Or you can set the buffering where you open the file to be line buffering.

So where you say:

# Im assuming you open your file like this since your code is
# an incomplete snippet. Otherwise tell us how you open your file
o = open('output_file.log', 'w')  

You must say:

o = open('output_file.log', 'w', buffering=1)  # enable line buffering

You should read the help of then open command by typing help(open) in the interactive python shell. It explains a great deal how buffering works in python.

dopstar
  • 1,406
  • 10
  • 20