如何使用 ftplib 正确下载文件以便为 Windows 添加换行符 [英] How to correctly download files using ftplib so line breaks are added for windows

查看:63
本文介绍了如何使用 ftplib 正确下载文件以便为 Windows 添加换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来我一直使用一个非常简单的批处理文件从 UNIX ftp 服务器下载数百万个文件

I have been using a very simple batch file to download millions of files from a UNIX ftp server for years

login
passwd
ascii
prompt n
cd to the right directory
get some_file
get another_file
cd to the next directory
repeat the pattern

这样做的好处是它很简单,所有文件都带有 Window 的换行符,因此这些文件可以与我现有的程序一起使用.由于我的路由器发生了一些变化,我不得不编写一个 Python 脚本来提取文件——我的第一个版本的脚本非常简单——但它有效

The nice thing about this was that it was simple and all the files arrived with Window's line breaks so the files were ready to use with my existing programs. Because of some changes in my router I had to write a Python script to pull the files - my first version of the script is very simple - but it works

for key in key_filings:
   for filing in key_filings[key]:
        remote_directory = '/foo/bar/' + key + '/' + filing['key_number']
        ftp.cwd(remote_directory)
        text_file = filing['txt']
        ftp.retrlines('RETR '+ text_file, open(save_dir + text_file,'w').writelines)
        hdr_file = filing['hdr']
        ftp.retrlines('RETR ' + hdr_file, open(save_dir + hdr_file,'w').writelines)

但是,这些文件没有任何明显的换行符.这些文件存储在 unix 系统中.在我使用 Windows CMD shell 下载文件之前,换行符就在那里.我曾尝试发送 ASCII 命令,但正如预期的那样没有任何效果.

However, the files do not have any apparent line breaks. The files are stored in a unix system. Before when I downloaded the files using the Windows CMD shell the line breaks were just there. I have tried sending the ASCII command but as expected that did not have any effect.

重要的是我能够访问最初存在的换行符,因为我的一些代码处理是基于行的.

It is critical that I be able to have access to the line breaks that existed originally as some of my code processing is line based.

推荐答案

通常,当我写出问题时,我就可以去找答案了.我想删除问题而不是回答它,但我认为可能还有像我这样的其他人可以使用答案,所以我将发布我从这个网页 作者 Fredrik Lundh.

Well as usually happens when I write a question out I can then go find the answer. I thought of deleting the question instead of answering it but I think there are probably others like me who could use the answer so I am going to post what I took away from this webpage by Fredrik Lundh.

我想保存文件而不是像在脚本中那样将其打印到屏幕

I want to save the file instead of printing it to the screen as done in that script

基本上,retrlines 是一次从服务器检索一行(在下面的脚本中,我正在编写该行,并添加一个换行符.

Basically the retrlines is retrieving one line at a time from the server (s in the script below I am writing the line as it arrives with the addition of a newline character.

我不太了解 lamda 函数或回调是什么,所以这是我最终围绕这些概念进行思考的借口.

I don't really understand lamda functions or what callbacks are so this is an excuse to finally wrap my head around those concepts.

import ftplib
ftp = ftplib.FTP('ftp.some.site', user = 'username', passwd = 'password_for_username')

for key in key_filings:
    for filing in key_filings[key]:
        remote_directory = '/foo/bar/' + key + '/' + filing['key_number']
        ftp.cwd(remote_directory)
        text_file = filing['txt']
        save_text_ref = open(save_dir + text_file, 'w')
        ftp.retrlines('RETR '+ text_file, lambda s, w = save_text_ref.write: w(s+'\n'))
        save_text_ref.close()
        hdr_file = filing['hdr']
        save_hdr_ref = open(save_dir +hdr_file,'w')
        ftp.retrlines('RETR ' + hdr_file, lambda s, w = save_hdr_ref.write: w(s+'\n'))
        save_hdr_ref.close()

这篇关于如何使用 ftplib 正确下载文件以便为 Windows 添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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