Download text files using Python and ftplib.FTP from z / os

I am trying to automate loading some text files from PDS z / os using Python and ftplib.

Since the host files are EBCDIC, I cannot just use FTP.retrbinary ().

FTP.retrlines () when used with an open (file, w) .writelines as a callback, of course, does not provide EOL.

So, for starters, I came up with this piece of code that “looks good to me,” but since I'm relative Python noob, can anyone suggest a better approach? Obviously, to keep this question simple, this is not the final thing that sounds and sounds.

Many thanks.

#!python.exe
from ftplib import FTP

class xfile (file):
    def writelineswitheol(self, sequence):
        for s in sequence:
            self.write(s+"\r\n")

sess = FTP("zos.server.to.be", "myid", "mypassword")
sess.sendcmd("site sbd=(IBM-1047,ISO8859-1)")
sess.cwd("'FOO.BAR.PDS'")
a = sess.nlst("RTB*")
for i in a:
    sess.retrlines("RETR "+i, xfile(i, 'w').writelineswitheol)
sess.quit()

Update: Python 3.0, the MingW platform for Windows XP.

z/os PDS , . , FTP- z/os , ().

, (, ):

import ftplib
import os
from sys import exc_info

sess = ftplib.FTP("undisclosed.server.com", "userid", "password")
sess.sendcmd("site sbd=(IBM-1047,ISO8859-1)")
for dir in ["ASM", "ASML", "ASMM", "C", "CPP", "DLLA", "DLLC", "DLMC", "GEN", "HDR", "MAC"]:
    sess.cwd("'ZLTALM.PREP.%s'" % dir)
    try:
        filelist = sess.nlst()
    except ftplib.error_perm as x:
        if (x.args[0][:3] != '550'):
            raise
    else:
        try:
            os.mkdir(dir)
        except:
            continue
        for hostfile in filelist:
            lines = []
            sess.retrlines("RETR "+hostfile, lines.append)
            pcfile = open("%s/%s"% (dir,hostfile), 'w')
            for line in lines:
                pcfile.write(line+"\n")
            pcfile.close()
        print ("Done: " + dir)
sess.quit()

,

+3
3

, , z/OS. python script , ebcdic . :

def writeline(line):
    file.write(line + "\n")

file = open(filename, "w")
ftp.retrlines("retr " + filename, writeline)
+3

( retrbinary) codecs EBCDIC . EBCDIC, z/OS (, cp500). , - ( UTF-8):

file = open(ebcdic_filename, "rb")
data = file.read()
converted = data.decode("cp500").encode("utf8")
file = open(utf8_filename, "wb")
file.write(converted)
file.close()

: retrlines, , , , , sequence , for , . , , self.write(sequence + "\r\n"), for. - file, , , , bells-and-whistles.

+3

writelineswitheol '\ r\n' '\n', , . , , , "\ r". '\n', .

" ". , open() try/except , try/except, callback_obj.close(), , retrlines() file_handle.close() ( try/except) - , " ( | | ) X, Y" , , .

Python 3.x ftplib.FTP.retrlines() str-, Unicode, - 1, Windows. (1) 256 (2) , EBCDIC.

[ ]

  • Python 3.0 ( " " ) 3.1.

  • , "i" FORTRAN 3 : -)

  • The two problems discovered so far (adding a line terminator to each character, an incorrect line terminator) were shown the first time you tested it.

+1
source

Source: https://habr.com/ru/post/1713580/


All Articles