[Tutor] FTPing a file while keeping Line Feeds
Fri Apr 11 18:58:02 2003
On Fri, 11 Apr 2003, Alan Colburn wrote:
> This is an easy question, but I can't seem to find the
> answer! After creating an ftplib.FTP object called
> 'ftp' and opening a file for writing, I download a
> text file via the command:
> ftp.retrlines('RETR textFile', file.write)
> textFile transfers to file, but textFile's line feeds don't transfer.
Just to make sure: by "line feeds", do you mean the carriage return
If so, then the behavior that we're running into is expected: FTP text
transfers are supposed to translate line endings to the one that your
computer natively uses.
As a concrete example: if the file originally had '\n', and we run the
program to download the file to our Windows machine, FTP will
transparently translate all the '\n' to '\r\n'.
> How do I transfer textFile such that the line feeds are kept?
> (Specifically, textFile contains comma separated values, with each line
> in the file representing a different row in a spreadsheet.)
In this case, if we want to keep the file verbatim, with no line-ending
translation, we should use the 'retrbinary' binary mode download. In
binary mode, a file is treated as a stream of bytes, not lines, so it does
> p.s. If you've got a quick reference to where I SHOULD have been looking
> [online] for this info, that too would be great.
Ideally, the official documentation at:
should have given a few good examples of common uses of ftplib. The first
link does show an example of retreiving a file with retrbinary, but
doesn't really explain the subtleties of text-vs-binary transfers.
If you feel that the documentation should be improved to explain this in
detail, perhaps one of us here on Tutor can write something short, and
send it over to the python-doc SIG.
Good luck to you!