ftp.retrbinary() 帮助 python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4696413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ftp.retrbinary() help python
提问by fabio.geraci
I have created a python script to connect to a remserver.
我创建了一个 python 脚本来连接到 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')
but this lines returns an error:
但这行返回错误:
typeerror: retrbinary() takes at least 3 arguments (2 given)
not sure what is looking for?
不知道在找什么?
回答by Joe Holloway
It's telling you that you aren't supplying enough arguments to the retrbinarymethod.
它告诉您没有为该retrbinary方法提供足够的参数。
The documentation specifiesthat 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.)
该文档指定您还必须提供一个“回调”函数,该函数为接收到的每个数据块调用。您将需要编写一个回调函数并使用它提供给您的数据做一些事情(例如将其写入文件,将其收集在内存中等)
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 ftpobject reference.
作为旁注,您可能会问为什么它说需要“3”个参数而不是“2”。这是因为它还计算 Python 在实例方法上需要的“self”参数,但是您隐式地将它与ftp对象引用一起传递。
EDIT- Looks like I may not have entirely answered your question.
编辑- 看起来我可能没有完全回答你的问题。
For the commandargument you are supposed to be passing a valid RETR command, not a list.
对于command参数,您应该传递有效的 RETR 命令,而不是列表。
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)
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.
对于callback,您需要传递接受单个参数的可调用对象(通常是某种函数)。参数是正在检索的文件中的数据块。我说“块”是因为当您四处移动大文件时,您很少希望将整个文件保存在内存中。该库旨在在接收数据块时迭代调用您的回调。这允许您写出文件的块,因此您在任何给定时间只需在内存中保留相对少量的数据。
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:
我这里的例子有点高级,但是你的回调可以是 for 循环内的一个闭包,它写入一个已经打开的文件:
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)
This can also be done more concisely with a lambdastatement, 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:
这也可以用lambda语句更简洁地完成,但我发现 Python 新手及其一些函数式概念更容易理解第一个示例。然而,这里是使用 lambda 的 ftp 调用:
ftp.retrbinary('RETR %s' % filename, lambda data: f.write(data))
I suppose you could even do this, passing the writeinstance method of the file directly as your callback:
我想你甚至可以这样做,write直接传递文件的实例方法作为你的回调:
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.
另外,我没有测试上述任何代码,所以如果它不起作用让我知道,我会看看我是否可以澄清它。

