Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You write:</p> <pre><code>outputfile.writelines sorted(data, key=len, reverse=True)[:3] </code></pre> <blockquote> <p>generates nothing.</p> </blockquote> <p>it should actually generate a syntax-error exception because of the missing parentheses -- if it doesn't, I guess you must be using some (allegedly) "smart" IDE like iPython which puts in the parentheses on your behalf -- is that the case?</p> <p>You're printing the three shortest lines, and they well may all be empty -- that should put three empty lines in your output file (and of course show nothing to stdout -- is that what you mean by "generates nothing"?). Maybe you're not properly calling <code>outputfile.close()</code> so by the time you check the file they're still buffered and not written to disk yet.</p> <p>As you see there are a huge number of possibilities around your very ambiguous "generates nothing" assertion. Can you clarify <strong>exactly</strong> what environment you're using and how in particular you're checking what your code "generates" or doesn't? Otherwise, it's very hard to help you much.</p> <p><strong>Edit</strong>: the OP clarified and showed his code -- and the problem is now clear: he's totally consuming <code>inputfile</code> the first time, never "rewinding" it, so the file immediately ends (no lines left in it) at the second <code>readlines</code> call. If you do need to read the file twice independently (rather than just reading it once and passing the data around, as would be normal), you'll need a call <code>inputfile.seek(0)</code> to "rewind the file" each time you read it to make it ready to be read all over again.</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.
    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