从z / os下载Python和ftplib.FTP的文本文件 [英] Downloading text files with Python and ftplib.FTP from z/os

查看:264
本文介绍了从z / os下载Python和ftplib.FTP的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



由于主机文件是EBCDIC,所以我可以使用Python和ftplib自动从z / os PDS下载一些文本文件。 t只需使用FTP.retrbinary()。



FTP.retrlines()与open(file,w).writelines作为回调使用时,当然不会提供EOL。



因此,对于初学者来说,我已经提出了一段对我来说很好的代码,但由于我是相对的Python noob,任何人都可以提出更好的方法?很明显,为了保持这个问题的简单性,这不是最后的,花里胡哨的事情。



非常感谢。

 #!python.exe 
from ftplib import FTP

class xfile(file):
def writelineswitheol(self,序列):
for s序列:
self.write(s +\r\\\


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 a:
sess.retrlines(RETR+ i,xfile(i,'w')。writelineswitheol)
sess.quit()

更新:Python 3.0,平台是Windows XP下的MingW。 / p>

z / os PDS具有固定的记录结构,而不是依靠行结尾作为记录分隔符。但是,z / os FTP服务器在文本模式下传输时会提供记录结束,后退()会剥离。



关闭更新

这是我的修改解决方案,对于正在进行的开发(例如移除内置密码):

  import ftplib 
import os
from sys import exc_info

sess = ftplib.FTP(undisclosed.server.com,userid,password)
sess.sendcmd(site sbd =(IBM-1047 ,ASM,ASML,ASMM,C,CPP,DLLA,DLLC,DLMC,GEN,ISO8859-1))
。 ,HDR,MAC]:
sess.cwd('ZLTALM.PREP。%s'%dir)
尝试:
filelist = sess.nlst()
,除了ftplib.error_perm为x:
if(x.args [0] [:3]!='550'):
raise
else:
try:
os.mkdir(dir)
除外:
继续
用于filelist中的hostfile:
lines = []
sess.retrlines(RETR+ hostfile ,lines.append)
pcfile = open(%s /%s%(dir,ho '$'
对于行中的行:
pcfile.write(行+\ n)
pcfile.close()
print(完成: + dir)
sess.quit()

感谢John和Vinay <当我试图找出如何从z / OS递归下载数据集时,刚刚遇到了这个问题。我一直在使用一个简单的Python脚本来从大型机上下载ebcdic文件。它实际上只是做到这一点:

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


file =打开(文件名,w)
ftp.retrlines(retr+文件名,writeline)


I'm trying to automate downloading of some text files from a z/os PDS, using Python and ftplib.

Since the host files are EBCDIC, I can't simply use FTP.retrbinary().

FTP.retrlines(), when used with open(file,w).writelines as its callback, doesn't, of course, provide EOLs.

So, for starters, I've come up with this piece of code which "looks OK to me", but as I'm a relative Python noob, can anyone suggest a better approach? Obviously, to keep this question simple, this isn't the final, bells-and-whistles thing.

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, platform is MingW under Windows XP.

z/os PDSs have a fixed record structure, rather than relying on line endings as record separators. However, the z/os FTP server, when transmitting in text mode, provides the record endings, which retrlines() strips off.

Closing update:

Here's my revised solution, which will be the basis for ongoing development (removing built-in passwords, for example):

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()

My thanks to both John and Vinay

解决方案

Just came across this question as I was trying to figure out how to recursively download datasets from z/OS. I've been using a simple python script for years now to download ebcdic files from the mainframe. It effectively just does this:

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

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

这篇关于从z / os下载Python和ftplib.FTP的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆