Well as usually happens when I write a question out I can then go find the answer. I thought of deleting the question instead of answering it but I think there are probably others like me who could use the answer so I am going to post what I took away from this webpage by Fredrik Lundh.
I want to save the file instead of printing it to the screen as done in that script
Basically the retrlines is retrieving one line at a time from the server (s in the script below I am writing the line as it arrives with the addition of a newline character.
I don't really understand lamda functions or what callbacks are so this is an excuse to finally wrap my head around those concepts.
import ftplib
ftp = ftplib.FTP('ftp.some.site', user = 'username', passwd = 'password_for_username')
for key in key_filings:
for filing in key_filings[key]:
remote_directory = '/foo/bar/' + key + '/' + filing['key_number']
ftp.cwd(remote_directory)
text_file = filing['txt']
save_text_ref = open(save_dir + text_file, 'w')
ftp.retrlines('RETR '+ text_file, lambda s, w = save_text_ref.write: w(s+'\n'))
save_text_ref.close()
hdr_file = filing['hdr']
save_hdr_ref = open(save_dir +hdr_file,'w')
ftp.retrlines('RETR ' + hdr_file, lambda s, w = save_hdr_ref.write: w(s+'\n'))
save_hdr_ref.close()