問題描述
我創(chuàng)建了一個 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
如您所見,它創(chuàng)建了一個列表.現(xiàn)在我將此列表傳遞給
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
方法提供足夠的參數(shù).
It's telling you that you aren't supplying enough arguments to the retrbinary
method.
文檔規(guī)定您還必須提供一個 '每個接收到的數(shù)據(jù)塊都會調(diào)用回調(diào)函數(shù).您需要編寫一個回調(diào)函數(shù)并對它提供給您的數(shù)據(jù)做一些事情(例如,將其寫入文件、將其收集到內(nèi)存中等)
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"個必需參數(shù),而不僅僅是2".這是因為它還計算了 Python 對實例方法所需的 'self' 參數(shù),但您通過 ftp
對象引用隱式傳遞了該參數(shù).
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
參數(shù),您應(yīng)該傳遞一個有效的 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
,您需要傳遞接受單個參數(shù)的可調(diào)用的東西(通常是某種函數(shù)).參數(shù)是正在檢索的文件中的一大塊數(shù)據(jù).我說塊"是因為當您移動大文件時,您很少希望將整個文件保存在內(nèi)存中.該庫旨在在接收數(shù)據(jù)塊時迭代地調(diào)用您的回調(diào).這允許您寫出文件的塊,因此您只需在任何給定時間在內(nèi)存中保留相對少量的數(shù)據(jù).
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.
我這里的例子有點高級,但是你的回調(diào)可以是 for 循環(huán)中的一個閉包,它寫入一個已經(jīng)打開的文件:
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
語句更簡潔地完成,但我發(fā)現(xiàn) Python 新手和它的一些函數(shù)式概念更容易理解第一個示例.不過,這里是使用 lambda 的 ftp 調(diào)用:
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
實例方法直接作為回調(diào)傳遞:
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)
所有這三個例子都應(yīng)該是相似的,希望通過它們的追蹤可以幫助你理解發(fā)生了什么.
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.
這篇關(guān)于ftp.retrbinary() 幫助 python的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!