0

I have a function that prints to an output file

def drawfile(m):
    findcovers(m)
    i = 1
    for x in m.intervalm:
        print("===="+str(i)+"====", file = output)
        print(configtostr(x.config), file = output)
        print("covered by:" + listtostr(x.parents,"+"), file = output)
        print("covers:" + listtostr(x.children,"-"), file = output)
        drawf(x)
        i = i + 1

I've run the same script twice and both times the output file stops after printing print(configtostr(x.config), file = output) without printing the next line print("covered by:" + listtostr(x.parents,"+"), file = output), both times at the same entry in m.intervalm.

For reference

def listtostr(l,string=" "):
    result = string
    for x in l:
        result += (str(x) + string)
    return result

is a pretty simple function that I don't think is looping infinitely. The script does continue to compute several more long computations after printing this file, so I looked into print buffering. But I don't think this is happening since it's been 48 hours and the output file is still not updated, and there is a lot more that should be printing. I know from what has already been printed that the later entries of m.intervalm have been computed. Python hasn't given me any errors either.

I'm pretty stuck, and would appreciate any insight/help. Thanks in advance!

  • What is `outfile` and is it initialized? Where is it closed? – DYZ Nov 03 '20 at 20:46
  • I have `output = open("output.txt", "w")`. I don't close it. I should say that I have run this script on smaller examples (which still take a day to compute), and they give me the full output file. – Freddie Huang Nov 03 '20 at 20:49
  • The functionality of `listtostr(x.parents,"+")` is built-in: `'+'.join(x.parents)`. – Karl Knechtel Nov 03 '20 at 20:49
  • Thanks. I'm not too familiar with Python so I'm sure I'm rewriting a lot of things. In any case, I'm not convinced that's where my script is going wrong. – Freddie Huang Nov 03 '20 at 20:51
  • 1
    @KarlKnechtel: Only if x.parents is already strings. And his seems to want the joiner at the end, too. – Frank Yellin Nov 03 '20 at 20:51
  • If you do not close (or flush) an open file, the content is not guaranteed to show up when you open the file in another program. – DYZ Nov 03 '20 at 20:51
  • This is going to be silly, but worth checking. You're positive that `configtostr` (or something it calls) doesn't modify the variable `output`? That's really the only explanation that I can come up with. – Frank Yellin Nov 03 '20 at 20:52
  • I'm very sure `configtostr` doesn't modify `output`. I know this isn't really proof that it doesn't, but my output only stops printing after 7000+ entries from `m.intervalm`. In my other examples I've computed, I've been able to print out 19000 entries without any problem. – Freddie Huang Nov 03 '20 at 20:54
  • I'll try running this again with closing the file. I've run this script a lot of other times with no problems though... I'm curious now why it's important to close files. – Freddie Huang Nov 03 '20 at 20:55

0 Answers0