List files on SFTP server matching wildcard in Python using Paramiko

孤街醉人 提交于 2020-01-29 16:56:05

问题


import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', username='test1234', password='test')
path = ['/home/test/*.txt', '/home/test1/*.file', '/home/check/*.xml']
for i in path:

    for j in glob.glob(i):

        print j

client.close()

I am trying to list the wildcard files on remote server by using glob.glob. But glob.glob() is not working.

Using Python 2.6.

Remote server contains these files: /home/test1/check.file, /home/test1/validate.file, /home/test1/vali.file

Can anyone please help on this issue.


回答1:


glob will not magically start working with a remote server, just because you have instantiated SSHClient before.

You have to use Paramiko API to list the files, like SFTPClient.listdir:

import fnmatch

sftp = client.open_sftp()

for filename in sftp.listdir('/home/test'):
    if fnmatch.fnmatch(filename, "*.txt"):
        print filename

Side note: Do not use AutoAddPolicy. You lose security by doing so. See Paramiko "Unknown Server".



来源:https://stackoverflow.com/questions/49381899/list-files-on-sftp-server-matching-wildcard-in-python-using-paramiko

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