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

ftp.retrbinary() 幫助 python

ftp.retrbinary() help python(ftp.retrbinary() 幫助 python)
本文介紹了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.

這篇關于ftp.retrbinary() 幫助 python的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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:“浮動對象不可下標)
主站蜘蛛池模板: 免费精品 | 国产日韩欧美在线观看 | 久久婷婷色| 中文字幕福利视频 | 日韩免费在线视频 | 欧美高清视频 | 99爱视频 | 亚洲精品久久久久久久久久久 | 国产二区在线播放 | 一区二区三区在线 | 免费午夜视频在线观看 | 成人国产精品一级毛片视频毛片 | 亚洲精品乱码久久久久久按摩观 | 高清欧美性猛交 | 成人在线观看网址 | 91精品国产高清一区二区三区 | 蜜月aⅴ免费一区二区三区 99re在线视频 | 国产乱码精品1区2区3区 | 91色站 | 国产农村妇女毛片精品久久麻豆 | 久久人人爽人人爽 | 在线观看毛片网站 | 黄色免费网站在线看 | 亚洲一区 中文字幕 | 日本成人毛片 | 日屁视频 | 色婷婷亚洲一区二区三区 | 午夜精品一区二区三区在线 | 国产精品99久久久久 | 亚洲精品久久久久中文字幕欢迎你 | 欧美日韩精品久久久免费观看 | 久久国内精品 | 国产一区免费视频 | 欧美在线a | 九九九视频在线观看 | 国产成人99久久亚洲综合精品 | 日日日视频 | 在线观看av网站永久 | 中文字幕精品一区 | 秋霞影院一区二区 | 午夜国产一级片 |