This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: ftplib please revisit retrlines('RETR as it produces files without newlines
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.11
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, mckenzm
Priority: normal Keywords:

Created on 2021-11-08 01:49 by mckenzm, last changed 2022-04-11 14:59 by admin. This issue is now closed.

File name Uploaded Description Edit
crap2.txt mckenzm, 2021-11-08 01:49 downloaded file.
Messages (4)
msg405921 - (view) Author: Matthew H. McKenzie (mckenzm) * Date: 2021-11-08 01:49
Lib/  function retrlines

Inspired by documentation the following writes a file without line-endings:
from ftplib import FTP

with open('crap2.txt', 'w') as fp:
    ftp.retrlines('RETR crap.txt', fp.write)

Code goes to pains to slice off the line endings, and then print_line does not add them back? Apologies if this has been covered before, or I am not following the documentation correctly. Not going to suggest a fix as there may be a reason it is like this.

For ascii
msg405926 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-11-08 06:31
Please tell us:

- What the file contains, and what you're expecting it to contain.
- What system are you running on?

crap2.txt (the source file) is a single line, so I'm not sure what you're expecting to happen.
msg405929 - (view) Author: Matthew H. McKenzie (mckenzm) * Date: 2021-11-08 06:49
On the face of it it is my mistake for using the write method for my file. But read on.

your write_line() adds an EOL, OK, because it wraps print().

So the retrlines() function strips them in anticipation?  

The error is arguably in my own code as I am passing write() as the callback and it is my fault for not adding line endings back?  Nothing at all wrong with write_line as the callback, it echoes perfectly, to the console.

But my files have no EOL.

I need the EOL in my file writes and that is my own problem. It would be maybe be an enhancement not to strip them from the host, as it should understand ascii as CRLF even if the client is not a CRLF system.

But that adds complexity - it needs to work as is for listings.

My uploaded file was what was being retrieved.

Maybe close this and I'll just do what everybody else does and clone retrlines().   

Sorry for your trouble.
msg405934 - (view) Author: Matthew H. McKenzie (mckenzm) * Date: 2021-11-08 07:52
To answer your original questions : Linux Host and Client, amd MVS (EBCDIC records) to Linux.

hacks to overcome (in libftp):

def print_line(line):
    '''Default retrlines callback to print a line.'''
    print(line, end='')            <==== suppress here

               if not line:
                if line[-2:] == CRLF:   <== left these
                    line = line[:-2]
                elif line[-1:] == '\n':
                    line = line[:-1]
                callback(line + '\n')  <== added it back here.
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89909
2021-11-08 07:52:26mckenzmsetmessages: + msg405934
2021-11-08 06:49:54mckenzmsetstatus: open -> closed
resolution: wont fix
messages: + msg405929

stage: resolved
2021-11-08 06:31:42eric.smithsetnosy: + eric.smith
messages: + msg405926
2021-11-08 01:49:30mckenzmcreate