久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

ftp.retrbinary() 幫助 python

ftp.retrbinary() help python(ftp.retrbinary() 幫助 python)
本文介紹了ftp.retrbinary() 幫助 python的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我創(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標)
主站蜘蛛池模板: 91在线视频精品 | 毛片一区二区三区 | 国产精品视频一区二区三区不卡 | 精品国产一区二区三区观看不卡 | 另类 综合 日韩 欧美 亚洲 | av一区在线观看 | 久久一区二区三区四区五区 | 日韩精品一区二区三区视频播放 | 最近中文字幕在线视频1 | 美女黄网 | 91在线视频免费观看 | 久久久精品视频免费看 | 九九久久精品 | 国产一区二区三区四区在线观看 | 四虎最新视频 | 国产中文一区二区三区 | 欧美日韩国产不卡 | 日韩欧美福利视频 | 人人人人干 | 国产一区欧美 | 国产精品网址 | 538在线精品 | 中文久久 | 国产精品日本一区二区在线播放 | 在线看无码的免费网站 | 日本网站免费在线观看 | 中文字幕av亚洲精品一部二部 | 国产成人综合久久 | 色欧美片视频在线观看 | 亚洲一级毛片 | 亚洲一区二区三区在线视频 | 黄色毛片视频 | av网站免费看 | 欧美视频在线播放 | 国产精品91视频 | 91精品国产91久久久久久吃药 | 欧美成年人视频在线观看 | 一区二区视屏 | 欧美不卡视频 | 欧美一级欧美三级在线观看 | 激情婷婷成人 |