How to fetch sizes of all SFTP files in a directory through Paramiko

后端 未结 1 752
说谎
说谎 2020-12-11 11:46
import paramiko
from socket import error as socket_error
import os 
server =[\'10.10.0.1\',\'10.10.0.2\']
path=\'/home/test/\'
for hostname in server:
    try:
              


        
相关标签:
1条回答
  • 2020-12-11 11:56

    SFTPClient.listdir returns file names only, not a full path. So to use the filename in another API, you have to add a path:

    for i in sftp.listdir(path):
        info = sftp.stat(path + "/" + i)
        print info.st_size     
    

    Though that's inefficient. Paramiko knows the size already, you are just throwing the information away by using SFTPClient.listdir instead of SFTPClient.listdir_attr (listdir calls listdir_attr internally).

    for i in sftp.listdir_attr(path):
        print i.st_size  
    
    0 讨论(0)
提交回复
热议问题