Python open FTP url and write to file
<p>How can I open and FTP url and download it into a file. What I'm trying looks something like this:</p>

<pre><code>from contextlib import closing
from urllib.request import urlopen

url = 'ftp://whatever.com/file.txt'
target_path = 'file.txt'

with closing(urlopen(url)) as source:
    with open(target_path, 'wb') as target:
        target.write(source)
</code></pre>

<p>However, this fails with the following error:</p>

<pre><code>TypeError: 'addinfourl' does not support the buffer interface
</code></pre>

<p>Is there any simple way to make this work? Especially if I want to extend it, so that the file is extracted while it is downloaded?</p>