使用Paramiko在Python中列出与通配符匹配的SFTP服务器上的文件 [英] List files on SFTP server matching wildcard in Python using Paramiko
问题描述
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()
我正在尝试使用 glob.glob
列出远程服务器上的通配符文件.但是 glob.glob()
不起作用.
I am trying to list the wildcard files on remote server by using glob.glob
. But glob.glob()
is not working.
使用 Python 2.6.
Using Python 2.6.
远程服务器包含这些文件:/home/test1/check.file
、/home/test1/validate.file
、/home/test1/vali.file
Remote server contains these files: /home/test1/check.file
, /home/test1/validate.file
, /home/test1/vali.file
任何人都可以帮忙解决这个问题.
Can anyone please help on this issue.
推荐答案
glob
不会神奇地开始与远程服务器一起工作,仅仅因为您之前已经实例化了 SSHClient
.
The glob
will not magically start working with a remote server, just because you have instantiated SSHClient
before.
您必须使用 Paramiko API 来列出文件,例如 SFTPClient.listdir
:
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
如果更适合您的需要,您也可以使用正则表达式进行匹配.请参阅使用 Paramiko 的 SFTPClient 在远程路径中使用通配符.
You can also use a regular expression for the matching, if it suits your needs better. See Using wildcard in remote path using Paramiko's SFTPClient.
旁注:不要使用 AutoAddPolicy
.你这样做会失去安全性.请参阅Paramiko未知服务器".
Side note: Do not use AutoAddPolicy
. You
lose security by doing so. See Paramiko "Unknown Server".
这篇关于使用Paramiko在Python中列出与通配符匹配的SFTP服务器上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!