pythonurllibsixurlretrieve

close the ftp connection after using urlretrieve


from six.moves.urllib.request import urlopen, urlretrieve    
urlretrieve('ftp://ftp.cdc.noaa.gov/Datasets/ncep.reanalysis.dailyavgs/surface/air.sig995.1949.nc', filename='C:\\desktop\\')

Do I need to close the ftp connection after using urlretrieve like this? If yes, then how?


Solution

  • No, urlretrievewill do that for you. If you were using urlopen, you should/could close the connection (see this question), but urlretrieve handles this four you as a convenient wrapper.

    Internally, urlretrieve uses an URLOpener instance, which calls close when its reference count drops to 0, which is at the end of the urlretrieve call.