Note that there are some explanatory texts on larger screens.

plurals
  1. POException while handling generator.send() in try...except block
    primarykey
    data
    text
    <pre><code>filename = 'tempfile' def tail(filename): fd = open(filename) while True: line = fd.readline() if not line: continue else: if filename != 'uh': yield line else: print 'Returning f to close the file' yield fd try: genObj = tail(filename) valfromgen= genObj.next() while valfromgen: print valfromgen valfromgen= genObj.next() except: traceback.print_exc() try: fd_Got_Back = genObj.send('uh') fd_Got_Back.close() except: traceback.print_exc() </code></pre> <hr> <p>Intention of the code: I have opened the file in the generator function only and not outside it, but, I want to close that file outside the generator function by using 'send' probably.</p> <p>What I am trying to do: Replicating <code>tail -f</code> from unix. </p> <p>How I am trying to do:</p> <ol> <li>Open a tempfile in read mode.</li> <li>If the tempfile has 1 new line written in it <em>(which I'll keep on writing manually and saving tempfile using notepad)</em>, yield the newly written line.</li> </ol> <p>Problem:</p> <p>The problem is that I'm trying to check how I can close the opened tempfile from this python code if I press <kbd>Ctrl</kbd> + <kbd>C</kbd> <em>(i.e. SIGTERM)</em> when this Python code runs in command prompt. In order to emulate this, I have opened the tempfile in the <code>tail</code> function, and whenever there is an exception <em>(which will be raised by the system when I press <kbd>Ctrl</kbd> + <kbd>C</kbd>)</em>, the control should go in the 1<sup>st</sup> except. Then, from here, I'm trying to send a value <code>uh</code> to the generator function <code>tail</code>, so that it should yield the file descriptor of the opened file which I can use to close the opened tempfile.</p> <p>PS: I expect a solution where I have opened the file in the generator function only and not outside it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload