pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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ù).我說塊"是因為當(dāng)您移動大文件時,您很少希望將整個文件保存在內(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:“浮動對象不可下標(biāo))
主站蜘蛛池模板: 首页|光催化反应器_平行反应仪_光化学反应仪-北京普林塞斯科技有限公司 | 高楼航空障碍灯厂家哪家好_航空障碍灯厂家_广州北斗星障碍灯有限公司 | 旅游规划_旅游策划_乡村旅游规划_景区规划设计_旅游规划设计公司-北京绿道联合旅游规划设计有限公司 | 番茄畅听邀请码怎么输入 - Dianw8.com | 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | 西宁装修_西宁装修公司-西宁业之峰装饰-青海业之峰墅级装饰设计公司【官网】 | CPSE安博会 | 数码听觉统合训练系统-儿童感觉-早期言语评估与训练系统-北京鑫泰盛世科技发展有限公司 | 常州翔天实验仪器厂-恒温振荡器-台式恒温振荡器-微量血液离心机 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 耐磨陶瓷管道_除渣器厂家-淄博浩瀚陶瓷科技有限公司 | 浙江栓钉_焊钉_剪力钉厂家批发_杭州八建五金制造有限公司 | 青岛成人高考_山东成考报名网 | 球磨机,节能球磨机价格,水泥球磨机厂家,粉煤灰球磨机-吉宏机械制造有限公司 | 亚洲工业智能制造领域专业门户网站 - 亚洲自动化与机器人网 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 最新范文网_实用的精品范文美文网 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 北京开业庆典策划-年会活动策划公司-舞龙舞狮团大鼓表演-北京盛乾龙狮鼓乐礼仪庆典策划公司 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 硅PU球场、篮球场地面施工「水性、环保、弹性」硅PU材料生产厂家-广东中星体育公司 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 水质传感器_水质监测站_雨量监测站_水文监测站-山东水境传感科技有限公司 | 丹佛斯压力传感器,WISE温度传感器,WISE压力开关,丹佛斯温度开关-上海力笙工业设备有限公司 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 涡街流量计_LUGB智能管道式高温防爆蒸汽温压补偿计量表-江苏凯铭仪表有限公司 | 走心机厂家,数控走心机-台州博城智能科技有限公司 | 合肥升降机-合肥升降货梯-安徽升降平台「厂家直销」-安徽鼎升自动化科技有限公司 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 江西自考网| 万博士范文网-您身边的范文参考网站Vanbs.com | 杭州可当科技有限公司—流量卡_随身WiFi_AI摄像头一站式解决方案 | B2B网站_B2B免费发布信息网站_B2B企业贸易平台 - 企资网 | 安驭邦官网-双向万能直角铣头,加工中心侧铣头,角度头[厂家直销] 闸阀_截止阀_止回阀「生产厂家」-上海卡比阀门有限公司 | 企小优-企业数字化转型服务商_网络推广_网络推广公司 | 回转支承-转盘轴承-回转驱动生产厂家-洛阳隆达轴承有限公司 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | HYDAC过滤器,HYDAC滤芯,现货ATOS油泵,ATOS比例阀-东莞市广联自动化科技有限公司 | 浙江红酒库-冰雕库-气调库-茶叶库安装-医药疫苗冷库-食品物流恒温恒湿车间-杭州领顺实业有限公司 | 蜗轮丝杆升降机-螺旋升降机-丝杠升降机厂家-润驰传动 | 昆明挖掘机修理厂_挖掘机翻新再制造-昆明聚力工程机械维修有限公司 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 |