print'Changing directory to {}'.format(config['ftp']['path'])
ftp.cwd(config['ftp']['path'])
print'Getting list of files from server'
filelst=ftp.nlst()
returnfilelst
deffileDL(filelst):
print'Downloading files from server'
tmpfilelst=[]
# Download all the files
foriinfilelst:
tmpfile=tempfile.mkstemp(dir=savedir)# The file will not be deleted when this script runs because we created the file directly instead of using TemporaryFile()
tmpfilelst.append(tmpfile)
withopen(tmpfile[1],'wb')astmpfile:# tmpfile is a tuple, second half contains filename. Open as binary file
# retrlines does not add newline characters per line that it downloads and seriously screws up CSV
printftp.retrlines('RETR '+i,lambdas,w=tmpfile.write:w(s+'\n'))# This lambda adds a newline to each line so it can be processed by CSV
returntmpfilelst
defmakeHash(thedir):
# Computes the MD5 for all files in a given directory and returns a list of lists containing the hash and path to file