With pysftp or Paramiko, how can I get a directory listing complete with attributes?

拜拜、爱过 提交于 2021-02-08 02:13:20

问题


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.

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.

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.

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)

回答1:


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

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.

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)

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))

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)]



回答2:


Related: For anyone coming across this in the future, if you want the linux-style dir listing in a string, start with the code sample at the top, and replace

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

with this instead:

    mydirlist=""
    for i in SFTP.listdir_attr():
        mydirlist = mydirlist + str(i) + "\n"
    mydirlist = mydirlist.rstrip()
    print(mydirlist)

This took me some figuring out, figured I'd share if anyone needs it in the future.



来源:https://stackoverflow.com/questions/58966394/with-pysftp-or-paramiko-how-can-i-get-a-directory-listing-complete-with-attribu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!