zoukankan      html  css  js  c++  java
  • Python Ftp Ftplib dir()方法 返回值问题

    最近在用Ftplib 模块中的 dir方法 ,想用变量存储一下 返回的目录,发现返回的是None ,看了下源码发现并没有返回值, 只是会默认输出到标准输出流,百度了一下 也没百度到解决方案。

    #Ftplib模块中 dir方法的定义 发现无返回值

        def dir(self, *args):
            '''List a directory in long form.
            By default list current directory to stdout.
            Optional last argument is callback function; all
            non-empty arguments before it are concatenated to the
            LIST command.  (This *should* only be used for a pathname.)'''
            cmd = 'LIST'
            func = None
            if args[-1:] and type(args[-1]) != type(''):
                args, func = args[:-1], args[-1]
            for arg in args:
                if arg:
                    cmd = cmd + (' ' + arg)
            self.retrlines(cmd, func)
    

    研究了一下,发现直接在Ftplib源码中新写一个dirs方法就可以解决返回值的问题:

    这里用了templist ,返回为所有目录的列表

        def dirs(self, *args):
            '''List a directory in long form.
            By default list current directory to stdout.
            Optional last argument is callback function; all
            non-empty arguments before it are concatenated to the
            LIST command.  (This *should* only be used for a pathname.)'''
            cmd = 'LIST'
            templist = []
            func = None
            if args[-1:] and type(args[-1]) != type(''):
                args, func = args[:-1], args[-1]
            for arg in args:
                if arg:
                    cmd = cmd + (' ' + arg)
            self.retrlines(cmd, templist.append)
            return templist
    
  • 相关阅读:
    Android客户端与服务器交互方式-小结
    个人工作总结01
    第7周学习进度
    第6周学习进度
    PHP_D4_“简易聊天室 ”的具体技术实现
    php_D3_“简易聊天室 ”实现的关键技术 详解
    团队介绍
    最大联通子数组
    构建之法阅读笔记04
    大道至简阅读笔记04
  • 原文地址:https://www.cnblogs.com/pandaa/p/12121378.html
Copyright © 2011-2022 走看看