搜索此博客

2018年7月2日星期一

Download tar compressed files from FTP using Python

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 01 13:00:26 2017

@author: Baikal
"""
from ftplib import FTP
import os
import urllib2

def Download_CIS( Root_dir, Sub_dir, Position, Year, CIS_Root_DIR, CIS_SUB_DIR ):

    CIS_DIR = os.path.join( CIS_Root_DIR, Year, CIS_SUB_DIR )
    os.chdir( CIS_DIR )
    Web_Sub_dir = Sub_dir + '/' + Position + '/' + Year 
    
    ftp = FTP( Root_dir )
    #Log in anonymous
    ftp.login()
    # Change the directory.
    ftp.cwd( Web_Sub_dir ) 
    
    lists = ftp.nlst() 
    
    for list in lists:
        if list.endswith( '.tar' ):
            Website_file_dir = 'ftp://' + Root_dir + '/' + Web_Sub_dir + '/' + list
            CIS_data = urllib2.urlopen( Website_file_dir ).read(  )
            CIS_File = os.path.join( CIS_DIR, list )
            with open( CIS_File, 'wb' ) as Data:
                Data.write( CIS_data )
                print list + 'has written down'


##                
Root_dir = 'sidads.colorado.edu'
Sub_dir = 'pub/DATASETS/NOAA/G02171'
Position = 'Western_Arctic'
Year = '2018'


CIS_Root_DIR = r'G:\CIS_Sea_Ice_Chart_3573'
if Position == 'Western_Arctic':
    CIS_SUB_DIR = 'WA'
else:
    CIS_SUB_DIR = 'EA'
    
Download_CIS( Root_dir, Sub_dir, Position, Year, CIS_Root_DIR, CIS_SUB_DIR )


Reference:
https://stackoverflow.com/questions/13534807/python-ftplib-connection-error-gaierror


没有评论:

发表评论

LibSVM Chinese Brief Infroduction

Reference: [1]  https://blog.csdn.net/v_july_v/article/details/7624837 [2]  https://wenku.baidu.com/view/c402e983336c1eb91b375d37.html?fr...

  • Word (2)