""" station2lowell - send most recent FTPDetectInfo results to Lowell ftp server v1.0: 2018-10-04, nmosko@lowell.edu """ from ftplib import FTP import datetime as dt import os import fnmatch import platform import ntpath def find_ftpdetectinfo(search_path): transfer_files=[] #For loop that defines the 'walk' of subdirs and files in source path for path, subdirs, files in os.walk(search_path): for file in files: #Find FTPDetectInfo file in directory if fnmatch.fnmatch(file, 'FTPDetectInfo*scanned.txt'): [prefix,camera,year,month,day,hour,minute,sec,suffix] = file.split('_') file_date = dt.datetime.strptime(year+month+day,'%Y%m%d') #If the file was created today, transfer to server if dt.datetime.today().date() == file_date.date(): transfer_files.append(path + '\\' +file) return transfer_files def ftpsend(local_path_to_file, filename, dir): #Local file to send - ascii file file = open(local_path_to_file, 'rb') #Connection address to Lowell FTP Server ftp = FTP('ftp.lowell.edu') #Open connection user = 'locams' password = 'Locamsftp@dm1n' ftp.login(user, password) ftp.cwd(dir) #transfer file ftp.storlines('STOR ' +filename, file) print(filename+' transfered to '+dir) #close connection ftp.close() #Close file file.close() return if __name__ == '__main__': #Source path search_path = 'e:/' #retrieve full path to files and file names full_path_files = find_ftpdetectinfo(search_path) filenames = [ntpath.basename(name) for name in full_path_files] #flags to transfer data transfer_flag = [1,1] #new remote directory computer = platform.node() newdir = '/ftp2/ftp/incoming/locams/data/'+computer+'/'+dt.date.today().strftime('%Y-%m-%d') ##test whether remote directory and files exist ## #connection address ftp = FTP('ftp.lowell.edu') #open connection user = 'locams' password = 'Locamsftp@dm1n' ftp.login(user,password) #check for directory name, create if doesn't exist dirs = ftp.nlst('/ftp2/ftp/incoming/locams/data/'+computer) if newdir not in dirs: ftp.mkd(newdir) #check whether files already exist, de-flag for transfer if present files = ftp.nlst(newdir) for i, filename in enumerate(filenames): if newdir+'/'+filename in files: transfer_flag[i] = 0 print(newdir+'/'+filename+' already exists.') #Close connection ftp.close() #Initiate transfer for those files not on server for i, transfer in enumerate(transfer_flag): if transfer == 1: sendfile = ftpsend(full_path_files[i],filenames[i],newdir)