下面我已经在我列出整个文件夹的代码中包含了我当前的尝试,运行文件并下载它们。但是,此方法不支持迭代子文件夹和文件夹。
def handleDownload(block):
file.write(block)
ddir='U:/Test Folder'
filedestination = 'U:/SWEModelConstruction/UnmaskedData'
t1= []
t2= []
t3= []
os.chdir(ddir)
ftp = FTP('sidads.colorado.edu')
ftp.login()
print ('Logging in.')
directory = '/pub/DATASETS/NOAA/G02158/unmasked/'
print ('Changing to ' + directory)
ftp.cwd(directory)
ftp.retrlines('LIST')
print ('Accessing files')
filenames = ftp.nlst() # get filenames within the directory
print (filenames)
for filename in filenames:
if filename not in ['.', '..']:
#Parse values from filename to use in os.path.join
for fname in filenames:
t1 = fname[16:20]
t2 = fname[20:22]
t3 = fname[22:24]
t4 = fname[16:24]
if not t1: continue
#use parsed values from filenamee to create folder and file pathss
local_folder = os.path.join(filedestination, t1,t2,t3)
local_filename = os.path.join(filedestination, t1,t2,t3,filename)
local_dat = os.path.join(filedestination, t1,t2,t3,'zz_ssmv11034tS__T0001TTNATS'+t4+'05HP001.dat.gz')
local_hdr = os.path.join(filedestination, t1,t2,t3,'zz_ssmv11034tS__T0001TTNATS'+t4+'05HP001.Hdr.gz')
hdrfile = ('zz_ssmv11034tS__T0001TTNATS'+t4+'05HP001.Hdr')
print (local_folder)
print (local_filename)
#check if folder for file exists and wether or not you have already it
if os.path.exists(local_folder) and not os.path.isfile(local_filename):
with open(local_filename, 'wb') as f_output:
ftp.retrbinary('RETR '+ filename, f_output.write)
ftp.quit()
答案 0 :(得分:-1)
我指导你Alvin's Big Data Notebook。你会在那里找到一个使用pysftp和walktree的例子。我不知道你正在使用什么FTP库,但如果你使用的是pysftp,这个例子应该有所帮助。关键是要了解如何使用walktree
。