如何在另一台通过SSH连接的计算机上进行
os.walk()
操作? 问题在于os.walk()
是在本地机器上执行,并且我想要ssh到另一个主机,遍历一个目录并为其中的每个文件生成MD5哈希值。
我目前编写的代码如下所示,但它没有生效。非常感谢您提供任何帮助。
try:
hash_array = []
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('sunbeam', port=22, username='xxxx', password='filmlight')
spinner.start()
for root, dirs, files in os.walk(_path):
for file in files:
file_path = os.path.join(os.path.abspath(root), file)
# generate hash code for file
hash_array.append(genMD5hash(file_path))
file_nb += 1
spinner.stop()
spinner.ok('Finished.')
return hash_array
except Exception as e:
print(e)
return None
finally:
ssh.close()
os.path.join
。os.path.join
使用本地文件系统约定,而SFTP始终使用正斜杠分隔符。 因此,您的代码在Windows上将失败。 另请参见Python pysftp put_r在Windows上不起作用。 - Martin Prikryl