2

Even though I'm using file.flush() at the end, no data will be written into the txt file.

# some essential code to connect to the server

while True:
    try:
        # do some stuff
        try:
            gaze_positions = filtered_surface['gaze_on_srf']
            for gaze_pos in gaze_positions:
                norm_gp_x, norm_gp_y = gaze_pos['norm_pos']

                if (0 <= norm_gp_x <= 1 and 0 <= norm_gp_y <= 1):
                    with open('/the/path/to/the/file.txt', 'w') as file:
                            file.write('[' + norm_gp_x + ', ' + norm_gp_y + ']')
                            file.flush()
                    print(norm_gp_x, norm_gp_y)
        except:
            pass
    except KeyboardInterrupt:
        break

What am I doing wrong? Obvisouly I miss something, but I can't figure it out, what it is. Another odd thing: there's even no output for print(norm_gp_x, norm_gp_y). If I put the with open ... in a comment, I'll get the output.

Unnamed
  • 93
  • 8
  • Are you 100% sure that your `if` statement ever evaluates to True and the `file.write` is ever executed? A [mcve] would help us diagnose the problem. – Aran-Fey Mar 19 '18 at 19:46
  • @Aran-Fey yes, I edited my question. Without writing something into the file, I'll get an output. – Unnamed Mar 19 '18 at 19:47
  • 3
    `except: pass` is a very lousy way of handling errors. If you don't get the print statement, that's because you got an exception. – Jean-François Fabre Mar 19 '18 at 19:47
  • @GarrettGutierrez it does print an output. – Unnamed Mar 19 '18 at 19:48
  • What is: `#do some stuff`? It is needed in the question, because answerers need to be able to replicate the problem. Otherwise, you get `NameError: name 'filtered_surface' is not defined`, and that only if you get rid of the `try/except: pass`. If you don't remove that, you don't get anything, obviously. – Artemis Mar 19 '18 at 20:05

1 Answers1

6

got it:

First

if (0 <= norm_gp_x <= 1 and 0 <= norm_gp_y <= 1):

then:

file.write('[' + norm_gp_x + ', ' + norm_gp_y + ']')

So you're adding strings and integers. This triggers an exception, and since you used an universal except: pass construct, the code skips every iteration (note that this except statement also catches the KeyboardInterrupt exception you're trying to catch at a higher level, so that doesn't work either)

Never use that construct. If you want to protect about a specific exception (ex: IOError), use:

try IOError as e:
   print("Warning: got exception {}".format(e))

so your exception is 1) focused and 2) verbose. Always wait until you get exceptions that you want to ignore to ignore them, selectively (read Catch multiple exceptions in one line (except block))

So the fix for your write it:

file.write('[{},{}]'.format(norm_gp_x, norm_gp_y))

or using the list representation since you're trying to mimic it:

file.write(str([norm_gp_x, norm_gp_y]))

Aside: your other issue is that you should use append mode

with open('/the/path/to/the/file.txt', 'a') as file:

or move you open statement before the loop, else you'll only get the last line in the file (a classic) since w mode truncates the file when opening. And you can drop flush since exiting the context closes the file.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219