python远程创建文件夹上传文件详解编程语言

python远程创建文件夹上传文件

 这里注意 路径/一定要完整 不然 os.path.join 默认都是/链接路径的

import paramiko
import os
ip=’1121′;
username=’12’;
password=’32333′;
transport = paramiko.Transport((ip, 22))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
ssh = paramiko.SSHClient();

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, port=22, username=username, password=password)

#上传文件目录
src_path = ‘C:/Users/Administrator/Desktop/ss’
target_path =’/root/ss’
ssh.exec_command(‘mkdir -p ‘+target_path);
def copy_file(src_path,target_path):
src_path=src_path+’/’;
target_path=target_path+’/’;
filelist = os.listdir(src_path)
for file in filelist:
path = os.path.join(src_path,file)
if os.path.isdir(path):
# 递归调用
target_path1 = os.path.join(target_path,file)
ssh.exec_command(‘mkdir -p ‘+target_path1)
print(‘pathx ‘+path)
print(‘target_path1 ‘+target_path1)
copy_file(path,target_path1)
else:
path1 = os.path.join(target_path,file)
print(‘pathxx ‘+path)
print(‘path1 ‘+path1)
sftp.put(path, path1)

copy_file(src_path,target_path)
transport.close();

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/18928.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论