ftp.retrbinary() 帮助 python
问题描述
我创建了一个 python 脚本来连接到 remserver.
I have created a python script to connect to a remserver.
datfile = []
for dk in range(len(files)):
dfnt=files[dk]
dpst=dfnt.find('.dat')
if dpst == 15:
dlist = dfnt[:]
datfile.append(dlist)
assert datfile == ['a.dat','b.dat']
# True
如您所见,它创建了一个列表.现在我将此列表传递给
Which as you can see creates a list. Now I am passing this list to
ftp.retrbinary('datfile')
但是这行返回错误:
typeerror: retrbinary() takes at least 3 arguments (2 given)
不确定要查找什么?
解决方案
它告诉你你没有为 retrbinary
方法提供足够的参数.
It's telling you that you aren't supplying enough arguments to the retrbinary
method.
文档规定您还必须提供一个 '每个接收到的数据块都会调用回调函数.您需要编写一个回调函数并对它提供给您的数据做一些事情(例如,将其写入文件、将其收集到内存中等)
The documentation specifies that you must also supply a 'callback' function that gets called for every block of data received. You'll want to write a callback function and do something with the data it gives you (e.g. write it to a file, collect it in memory, etc.)
作为旁注,您可能会问为什么它说有3"个必需参数,而不仅仅是2".这是因为它还计算了 Python 对实例方法所需的 'self' 参数,但您通过 ftp
对象引用隐式传递了该参数.
As a side note, you might ask why it says there are '3' required arguments instead of just '2'. This is because it's also counting the 'self' argument that Python requires on instance methods, but you are implicitly passing that with the ftp
object reference.
编辑 - 看起来我可能没有完全回答您的问题.
EDIT - Looks like I may not have entirely answered your question.
对于 command
参数,您应该传递一个有效的 RETR 命令,而不是一个列表.
For the command
argument you are supposed to be passing a valid RETR command, not a list.
filenames = ['a.dat', 'b.dat']
# Iterate through all the filenames and retrieve them one at a time
for filename in filenames:
ftp.retrbinary('RETR %s' % filename, callback)
对于 callback
,您需要传递接受单个参数的可调用的东西(通常是某种函数).参数是正在检索的文件中的一大块数据.我说块"是因为当您移动大文件时,您很少希望将整个文件保存在内存中.该库旨在在接收数据块时迭代地调用您的回调.这允许您写出文件的块,因此您只需在任何给定时间在内存中保留相对少量的数据.
For the callback
, you need to pass something that is callable (usually a function of some sort) that accepts a single argument. The argument is a chunk of data from the file being retrieved. I say a 'chunk' because when you're moving large files around, you rarely want to hold the entire file in memory. The library is designed to invoke your callback iteratively as it receives chunks of data. This allows you to write out chunks of the file so you only have to keep a relatively small amount of data in memory at any given time.
我这里的例子有点高级,但是你的回调可以是 for 循环中的一个闭包,它写入一个已经打开的文件:
My example here is a bit advanced, but your callback can be a closure inside the for loop that writes to a file which has been opened:
import os
filenames = ['a.dat', 'b.dat']
# Iterate through all the filenames and retrieve them one at a time
for filename in filenames:
local_filename = os.path.join('/tmp', filename)
# Open a local file for writing (binary mode)...
# The 'with' statement ensures that the file will be closed
with open(local_filename, 'wb') as f:
# Define the callback as a closure so it can access the opened
# file in local scope
def callback(data):
f.write(data)
ftp.retrbinary('RETR %s' % filename, callback)
这也可以使用 lambda
语句更简洁地完成,但我发现 Python 新手和它的一些函数式概念更容易理解第一个示例.不过,这里是使用 lambda 的 ftp 调用:
This can also be done more concisely with a lambda
statement, but I find people new to Python and some of its functional-style concepts understand the first example more easily. Nevertheless, here's the ftp call with a lambda instead:
ftp.retrbinary('RETR %s' % filename, lambda data: f.write(data))
我想你甚至可以这样做,将文件的 write
实例方法直接作为回调传递:
I suppose you could even do this, passing the write
instance method of the file directly as your callback:
ftp.retrbinary('RETR %s' % filename, f.write)
所有这三个例子都应该是相似的,希望通过它们的追踪可以帮助你理解发生了什么.
All three of these examples should be analogous and hopefully tracing through them will help you to understand what's going on.
为了举例,我省略了任何类型的错误处理.
I've elided any sort of error handling for the sake of example.
另外,我没有测试任何上述代码,所以如果它不起作用,请告诉我,我会看看我是否可以澄清它.
Also, I didn't test any of the above code, so if it doesn't work let me know and I'll see if I can clarify it.
相关文章