使用 pysftp 或 Paramiko,如何获得带有属性的目录列表? [英] With pysftp or Paramiko, how can I get a directory listing complete with attributes?

查看:60
本文介绍了使用 pysftp 或 Paramiko,如何获得带有属性的目录列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我正在尝试获取目录中所有文件和目录的列表,包括它们的属性(我正在寻找至少名称、大小、最后修改时间,它是文件还是一个文件夹).我在 Windows 上使用 Python 3.

As the title says, I'm trying to get a list of all the files and directories in a directory, including their attributes (I'm looking for at least name, size, last modified, and is it a file or a folder). I'm using Python 3 on Windows.

我试过 listdir(),我得到了一个没有属性的文件列表.我已经尝试了 listdir_attr(),我得到了一个属性列表,但没有文件名 - 我没有看到任何保证这两个列表的顺序相同的东西,到目前为止据我所知,我不能同时处理这两个列表.

I've tried listdir(), and I get a list of files without attributes. I've tried listdir_attr(), and I get a list of attributes, but no filenames - and I don't see anything that guarantees that those two lists will be in the same order, so as far as I know, I can't just process the two lists together.

即使我最终得到一个看起来像常规 FTP/Linux ls 列表的大字符串,也没关系,我可以稍后解析它.我只需要包含每个文件或文件夹的任何内容,并且至少需要我正在为每个文件或文件夹查找的属性.

Even if I just end up with a big string that looks like a regular FTP / Linux ls listing, that's fine, I can parse that later. I just need anything that has each file or folder and at minimum the attributes I'm looking for for each.

这是一个示例程序.连接值有效,可用于测试,是公共测试SFTP服务器.

Here's a sample program. The connection values are valid and can be used for testing, it's a public test SFTP server.

import pysftp

cnopts=pysftp.CnOpts()
# - I know this next line is insecure, it's just for this test program and
#    just to get a directory listing.
cnopts.hostkeys = None

print('Connecting...')
with pysftp.Connection('test.rebex.net', username='demo', password='password',
                    cnopts=cnopts) as SFTP:

    mydirlist = SFTP.??????
    #                ^^^^^^ What goes here?

print('Result:')
print(mydirlist)

推荐答案

pysftp Connection.listdir_attr(以及 Paramiko SFTPClient.listdir_attr,在它后面)返回一切.

The pysftp Connection.listdir_attr (as well as Paramiko SFTPClient.listdir_attr, which is behind it) returns everything.

如果你直接打印方法返回的列表,它不会打印名称,因为它的(错误?)方式__repr__ 方法 已实现.

If you directly print the list that the method returns, it does not print the names, due to the (wrong?) way its __repr__ method is implemented.

但是如果您打印单个元素,它会打印名称(如 __str__ 有更好的实现):

But if you print individual elements, it prints the names (as __str__ has better implementation):

files = sftp.listdir_attr(".")
for f in files:
    print(f)

要提取元素的文件名,请阅读SFTPAttributes.filename:

To extract element's filename, read SFTPAttributes.filename:

files = sftp.listdir_attr(".")
for f in files:
    t = datetime.datetime.fromtimestamp(f.st_mtime).strftime('%Y-%m-%dT%H:%M:%S')
    print("{}: {} {}".format(f.filename, f.st_size, t))


顺便说一句,listdirlistdir_attr 以相同的顺序返回完全相同的元素,如 Paramiko SFTPClient.listdir 内部仅调用 SFTPClient.listdir_attr:


Btw, listdir and listdir_attr returns the exactly same elements in the same order, as Paramiko SFTPClient.listdir internally only calls SFTPClient.listdir_attr:

return [f.filename for f in self.listdir_attr(path)]

这篇关于使用 pysftp 或 Paramiko,如何获得带有属性的目录列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆