bypy icon indicating copy to clipboard operation
bypy copied to clipboard

How to get list result

Open eeezae opened this issue 3 years ago • 2 comments

https://github.com/houtianze/bypy/wiki/%E5%A4%A7%E4%BD%AC%E4%BB%AC%E6%80%8E%E4%B9%88%E8%8E%B7%E5%8F%96%E5%88%B0list%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC%E5%95%8A

eeezae avatar May 29 '21 18:05 eeezae

通过监测打印简单实现了一下

    def list_files(self, file_path: str = '') -> list:
        from io import StringIO
        import sys
        res_io = StringIO()
        sys.stdout = res_io
        if not int(self.bdy.list(remotepath=file_path, fmt='$f')) == 0:
            return []
        output = res_io.getvalue()
        if output == '':
            return []
        res = output.split('\n')
        if len(res) > 0:
            del res[0]
        return res

eeezae avatar May 31 '21 06:05 eeezae

感谢, 有用! 有个小问题, 这样print()就失效了, 稍微改进了下:

def list_files(file_path: str = '') -> list:
    from io import StringIO
    import sys
    res_io = StringIO()
    temp = sys.stdout   #保存原IO映射关系
    sys.stdout = res_io
    if not int(bdy.list(remotepath=file_path, fmt='$f')) == 0:
        return []
    output = res_io.getvalue()
    sys.stdout = temp   #恢复默认IO映射关系
    if output == '':
        return []
    res = output.split('\n')
    if len(res) > 0:
        del res[0]
    return res

acliyang avatar Aug 13 '21 02:08 acliyang