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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM