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();

原文地址:https://www.cnblogs.com/newmiracle/p/13941834.html