简体   繁体   English

在 paramiko 中区分文件或目录

[英]Distinguish between a file or a directory in paramiko

I'm trying to walk over the contents of a directory and determine whether or not each item is a file or a folder.我试图遍历目录的内容并确定每个项目是文件还是文件夹。 I can do the solution suggested in this link :我可以执行此链接中建议的解决方案:

>>> for i in ftp.listdir():
...     lstatout=str(ftp.lstat(i)).split()[0]
...     if 'd' not in lstatout: print i, 'is a file'
... 

This works, yes.这行得通,是的。 As a sidenote, I'm just taking the first character instead of .split() , so str(ftp.lstati(i))[0] != 'd' , as this appears to be faster in my admittedly non-scientific testing.作为旁注,我只是取第一个字符而不是.split() ,所以str(ftp.lstati(i))[0] != 'd' ,因为这在我公认的非科学中似乎更快测试。

But this seems extremely hacky, so I tried to check out other methods.但这似乎非常hacky,所以我尝试检查其他方法。 In the SFTP attributes for a few folders, I see:在几个文件夹的 SFTP 属性中,我看到:

<SFTPAttributes: [ size=4096 uid=1002 gid=1002 mode=040755 atime=1358785383 mtime=1359475285 ]>

while in the same for files, I see:而在相同的文件中,我看到:

<SFTPAttributes: [ size=72 uid=1002 gid=1002 mode=0100644 atime=1367598914 mtime=1367598914 ]>

So it seems that the mode is 010[permissions] for files, and 040[permissions] for directories (consistent in the few directories I've seen, but can't say it's universal).所以看起来文件的模式是010[permissions] ,目录的模式是040[permissions] (在我见过的几个目录中是一致的,但不能说它是通用的)。 This seems to be a much better way to get that attribute of the item, But, when I use ftp.stat(i).st_mode , I get a different value – 16877 for the aforementioned directory, and 33188 for the aforementioned file.这似乎是获取项目属性的更好方法,但是,当我使用ftp.stat(i).st_mode时,我得到一个不同的值——上述目录为16877 ,上述文件为33188

What do these mean?这些是什么意思? Is there a way I can convert that integer to the file mode?有没有办法可以将 integer 转换为文件模式? (I tried Google, but can't find anything relevant.) Alternatively, is there any good way I can determine this without relying on the str(lstat(i)) function returning a 'd' or not-'d'? (我试过谷歌,但找不到任何相关的东西。)或者,有没有什么好方法可以在不依赖str(lstat(i)) function 返回“d”或不“d”的情况下确定这一点?

It looks like the high bits do indeed tell you whether it is a file or a directory: 看起来高位确实可以告诉您它是文件还是目录:

S_ISDIR S_ISDIR
S_ISREG S_ISREG

>>> oct(16877)
'040755'
>>> oct(33188)
'0100644'

And likewise: 同样:

>>> int('040755', 8)
16877
>>> int('0100644', 8)
33188

Paramiko's SFTPAttributes.st_mode can be passed to Python's os.stat for analysis: 可以将SFTPAttributes.st_modeSFTPAttributes.st_mode传递给Python的os.stat进行分析:

for file in ftp_cli.listdir_attr(path):
    is_dir = stat.S_ISDIR(file.st_mode)
    is_link = stat.S_ISLNK(file.st_mode)
    is_file = stat.S_ISREG(file.st_mode)

Python 3 Python 3

from paramiko.sftp_attr import SFTPAttributes
import stat

class FileInfo:
    def __init__(self, file: SFTPAttributes):
        self.name = file.filename
        self.is_dir = stat.S_ISDIR(file.st_mode)
        self.is_link = stat.S_ISLNK(file.st_mode)
        self.is_file = stat.S_ISREG(file.st_mode)
        self.size = file.st_size
        self.modified_on = file.st_mtime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM