windows上python上传下载文件到linux服务器指定路径【转】
从windows上传文件到linux,目录下的文件夹自动创建
#!/usr/bin/env python # coding: utf-8 import paramiko import datetime import os hostname='10.xxx.xxx.xx' username='username' password='***' port=22 def upload(local_dir,remote_dir): try: t=paramiko.Transport((hostname,port)) t.connect(username=username,password=password) sftp=paramiko.SFTPClient.from_transport(t) print('upload file start %s ' % datetime.datetime.now()) for root,dirs,files in os.walk(local_dir): print('[%s][%s][%s]' % (root,dirs,files)) for filespath in files: local_file = os.path.join(root,filespath) print(11,'[%s][%s][%s][%s]' % (root,filespath,local_file,local_dir)) a = local_file.replace(local_dir,'').replace('\\','/').lstrip('/') print('01',a,'[%s]' % remote_dir) remote_file = os.path.join(remote_dir,a) print(22,remote_file) try: sftp.put(local_file,remote_file) except Exception as e: sftp.mkdir(os.path.split(remote_file)[0]) sftp.put(local_file,remote_file) print("66 upload %s to remote %s" % (local_file,remote_file)) for name in dirs: local_path = os.path.join(root,name) print(0,local_path,local_dir) a = local_path.replace(local_dir,'').replace('\\','') print(1,a) print(1,remote_dir) remote_path = os.path.join(remote_dir,a) print(33,remote_path) try: sftp.mkdir(remote_path) print(44,"mkdir path %s" % remote_path) except Exception as e: print(55,e) print('77,upload file success %s ' % datetime.datetime.now()) t.close() except Exception as e: print(88,e) if __name__=='__main__': local_dir=r'D:\111' remote_dir='/home/share/111/' upload(local_dir,remote_dir)
windows上用python从linux服务器下载文件到指定路径
#!/usr/bin/env python
# coding: utf-8
import paramiko def remote_scp(host_ip,remote_path,local_path,username,password): t = paramiko.Transport((host_ip,22)) t.connect(username=username, password=password) sftp = paramiko.SFTPClient.from_transport(t) src = remote_path des = local_path sftp.get(src,des) t.close() if __name__ == '__main__': host_ip = '10.xxx.xxx.xx' remote_path = '/home/share/a.pdf' local_path = r'D:\aaa\a.pdf1' username = 'username' password = '******' remote_scp(host_ip,remote_path,local_path,username,password)
转自
windows上python上传文件到linux服务器指定路径 - CSDN博客
https://blog.csdn.net/weixin_40539892/article/details/79097386
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?