 <p>Since you reported that it was running when <code>len(nextList) == 0</code>, this is probably because <code>nextList</code> (which isn't a list..) is an empty bytes object which isn't equal to an empty string object:</p> <pre><code>&gt;&gt;&gt; b"" == "" False </code></pre> <p>and so the condition in your line</p> <pre><code>while nextList != "": </code></pre> <p>is never true, even when <code>nextList</code> is empty. That's why using <code>len(nextList) != 22</code> as a break condition worked, and even</p> <pre><code>while nextList: </code></pre> <p>should suffice.</p>