I am trying to read a file from an FTP site and process one line at a time. I write from the FTP server to a StringIO object and call the readline function, but this returns the entire file, rather that the first line. I downloaded the file to my pc and examined it with a hex editor, and the file uses x0d0a for a newline character, or a carriage return with a line feed. Could somebody point out to me where I might be going wrong here?
Thanks in advance!
#!/usr/bin/python
import ftplib
import StringIO
settles = StringIO.StringIO()
ftp = ftplib.FTP('ftp.cmegroup.com')
ftp.login()
ftp.cwd('pub/settle/')
ftp.retrlines('RETR cbt.settle.s.txt', settles.write)
settles.seek(0)
print settles.readline()
According to the FTP.retrlines
documentation:
... The callback function is called for each line with a string argument containing the line with the trailing CRLF stripped. ....
Replace retrlines
with retrbinary
.
Alternatively, you can ..retrlines ..
lines as follow (appending newlines):
ftp.retrlines('RETR cbt.settle.s.txt', lambda line: settles.write(line + '\n'))