問題描述
我正在嘗試使用 Python 和 ftplib 從 z/os PDS 自動(dòng)下載一些文本文件.
I'm trying to automate downloading of some text files from a z/os PDS, using Python and ftplib.
由于主機(jī)文件是 EBCDIC,我不能簡單地使用 FTP.retrbinary().
Since the host files are EBCDIC, I can't simply use FTP.retrbinary().
FTP.retrlines(),當(dāng)與 open(file,w).writelines 作為回調(diào)一起使用時(shí),當(dāng)然不提供 EOL.
FTP.retrlines(), when used with open(file,w).writelines as its callback, doesn't, of course, provide EOLs.
所以,對(duì)于初學(xué)者來說,我想出了這段對(duì)我來說看起來不錯(cuò)"的代碼,但由于我是一個(gè)相對(duì)的 Python 菜鳥,任何人都可以提出更好的方法嗎?顯然,為了讓這個(gè)問題保持簡單,這不是最終的花里胡哨的事情.
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.
非常感謝.
#!python.exe
from ftplib import FTP
class xfile (file):
def writelineswitheol(self, sequence):
for s in sequence:
self.write(s+"
")
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()
更新:Python 3.0,平臺(tái)為 Windows XP 下的 MingW.
Update: Python 3.0, platform is MingW under Windows XP.
z/os PDS 具有固定的記錄結(jié)構(gòu),而不是依賴行結(jié)尾作為記錄分隔符.但是,z/os FTP 服務(wù)器在以文本模式傳輸時(shí),會(huì)提供記錄結(jié)尾,而 retrlines() 會(huì)去掉這些結(jié)尾.
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.
結(jié)束更新:
這是我修改后的解決方案,它將成為持續(xù)開發(fā)的基礎(chǔ)(例如,刪除內(nèi)置密碼):
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+"
")
pcfile.close()
print ("Done: " + dir)
sess.quit()
感謝 John 和 Vinay
My thanks to both John and Vinay
推薦答案
我在試圖弄清楚如何從 z/OS 遞歸下載數(shù)據(jù)集時(shí)遇到了這個(gè)問題.多年來,我一直在使用簡單的 python 腳本從大型機(jī)下載 ebcdic 文件.它實(shí)際上就是這樣做的:
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 + "
")
file = open(filename, "w")
ftp.retrlines("retr " + filename, writeline)
這篇關(guān)于使用 Python 和 ftplib.FTP 從 z/os 下載文本文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!