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))
主站蜘蛛池模板: 电动不锈钢套筒阀-球面偏置气动钟阀-三通换向阀止回阀-永嘉鸿宇阀门有限公司 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 优考试_免费在线考试系统_培训考试系统_题库系统_组卷答题系统_匡优考试 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 冷藏车厂家|冷藏车价格|小型冷藏车|散装饲料车厂家|程力专用汽车股份有限公司销售十二分公司 | 电动葫芦-河北悍象起重机械有限公司 | 金属切削液-脱水防锈油-电火花机油-抗磨液压油-深圳市雨辰宏业科技发展有限公司 | 培训中心-翰香原香酥板栗饼加盟店总部-正宗板栗酥饼技术 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 玻璃钢型材-玻璃钢风管-玻璃钢管道,生产厂家-[江苏欧升玻璃钢制造有限公司] | 工业插头-工业插头插座【厂家】-温州罗曼电气 | SRRC认证_电磁兼容_EMC测试整改_FCC认证_SDOC认证-深圳市环测威检测技术有限公司 | 湖南成人高考报名-湖南成考网| 建筑资质代办-建筑资质转让找上海国信启航 | 阜阳成人高考_阜阳成考报名时间_安徽省成人高考网 | 众能联合-提供高空车_升降机_吊车_挖机等一站工程设备租赁 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 2025黄道吉日查询、吉时查询、老黄历查询平台- 黄道吉日查询网 | 珠宝展柜-玻璃精品展柜-首饰珠宝展示柜定制-鸿钛展柜厂家 | 深圳湾1号房价_深圳湾1号二手房源| 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 北京租车牌|京牌指标租赁|小客车指标出租 | 钢托盘,钢制托盘,立库钢托盘,金属托盘制造商_南京飞天金属制品实业有限公司 | 广州云仓代发-昊哥云仓专业电商仓储托管外包代发货服务 | 光伏支架成型设备-光伏钢边框设备-光伏设备厂家 | 隧道风机_DWEX边墙风机_SDS射流风机-绍兴市上虞科瑞风机有限公司 | 黑龙江京科脑康医院-哈尔滨精神病医院哪家好_哈尔滨精神科医院排名_黑龙江精神心理病专科医院 | 家用净水器代理批发加盟_净水机招商代理_全屋净水器定制品牌_【劳伦斯官网】 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 飞象网 - 通信人每天必上的网站 全球化工设备网—化工设备,化工机械,制药设备,环保设备的专业网络市场。 | 苏州伊诺尔拆除公司_专业酒店厂房拆除_商场学校拆除_办公楼房屋拆除_家工装拆除拆旧 | 电机修理_二手电机专家-河北豫通机电设备有限公司(原石家庄冀华高压电机维修中心) | 蔬菜清洗机_环速洗菜机_异物去除清洗机_蔬菜清洗机_商用洗菜机 - 环速科技有限公司 | 流变仪-热分析联用仪-热膨胀仪厂家-耐驰科学仪器商贸 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 纳米涂料品牌 防雾抗污纳米陶瓷涂料厂家_虹瓷科技 | 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | 深圳成考网-深圳成人高考报名网 深圳工程师职称评定条件及流程_深圳职称评审_职称评审-职称网 |