使用Paramiko在SFTP服务器上列出与Python中的通配符匹配的文件
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
.
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
旁注:请勿使用AutoAddPolicy
.你
这样做会失去安全性.参见 Paramiko未知服务器" .
Side note: Do not use AutoAddPolicy
. You
lose security by doing so. See Paramiko "Unknown Server".