我正在研究server 1
。我需要编写一个Python脚本来连接到server 2
并从一个目录中获取某些文件(名称以字母“HM”开头的文件)并将它们放入另一个目录中,该目录需要在运行时创建(因为对于程序的每次运行,都必须创建一个新目录)创建并且文件必须转储到那里),在server 1
.
我需要用 Python 来做这件事,而且我对这种语言还比较陌生。我不知道从哪里开始编写代码。有没有不涉及“压缩”文件的解决方案?我已经浏览过 Paramiko,但据我所知,它一次只传输一个文件。我什至看过 glob 但我不知道如何使用它。
要传输您可能想要查看 paramiko 的文件
import os
import paramiko
localpath = '~/pathNameForToday/'
os.system('mkdir ' + localpath)
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=username, password=password)
sftp = ssh.open_sftp()
sftp.get(remotepath, localpath)
sftp.close()
ssh.close()
如果你想使用 glob 你可以这样做:
import os
import re
import glob
filesiwant = re.compile('^HM.+') #if your files follow a more specific pattern and you don't know regular expressions you can give me a sample name and i'll give you the regex4it
path = '/server2/filedir/'
for infile in glob.glob( os.path.join(path, '*') ):
if filesiwant.match(infile):
print "current file is: " + infile
否则,更简单的替代方法是使用 os.listdir()
import os
for infile in os.listdir('/server2/filedir/'):
...`
这是否回答你的问题?如果不发表评论
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)