python用paramiko模块上传本地目录到远程目录
2016-02-22 09:33
603 查看
转自 : http://www.ithao123.cn/content-1214517.html
[摘要:python用paramiko模块默许中只能够上传文件,正在网上也出有找到适合的间接上传目次的方式,以是用os.walk方式战paramiko联合写了一个上传目次的方式,代码以下: 1 2 3 4 5 6 7 8 9 10 11 12 13 1]
python用paramiko模块默认中只可以上传文件,在网上也没有找到合适的直接上传目录的方法,所以用os.walk方法和paramiko结合写了一个上传目录的方法,代码如下:
#!/usr/bin/env python
import paramiko,datetime,os
hostname='192.168.1.100'
username='root'
password='123456'
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):
for filespath in files:
local_file = os.path.join(root,filespath)
a = local_file.replace(local_dir,'')
remote_file = os.path.join(remote_dir,a)
try:
sftp.put(local_file,remote_file)
except Exception,e:
sftp.mkdir(os.path.split(remote_file)[0])
sftp.put(local_file,remote_file)
print "upload %s to remote %s" % (local_file,remote_file)
for name in dirs:
local_path = os.path.join(root,name)
a = local_path.replace(local_dir,'')
remote_path = os.path.join(remote_dir,a)
try:
sftp.mkdir(remote_path)
print "mkdir path %s" % remote_path
except Exception,e:
print e
print 'upload file success %s ' % datetime.datetime.now()
t.close()
except Exception,e:
print e
if __name__=='__main__':
local_dir='/home/soft/'
remote_dir='/tmp/aaa/'
upload(local_dir,remote_dir)
[摘要:python用paramiko模块默许中只能够上传文件,正在网上也出有找到适合的间接上传目次的方式,以是用os.walk方式战paramiko联合写了一个上传目次的方式,代码以下: 1 2 3 4 5 6 7 8 9 10 11 12 13 1]
python用paramiko模块默认中只可以上传文件,在网上也没有找到合适的直接上传目录的方法,所以用os.walk方法和paramiko结合写了一个上传目录的方法,代码如下:
#!/usr/bin/env python
import paramiko,datetime,os
hostname='192.168.1.100'
username='root'
password='123456'
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):
for filespath in files:
local_file = os.path.join(root,filespath)
a = local_file.replace(local_dir,'')
remote_file = os.path.join(remote_dir,a)
try:
sftp.put(local_file,remote_file)
except Exception,e:
sftp.mkdir(os.path.split(remote_file)[0])
sftp.put(local_file,remote_file)
print "upload %s to remote %s" % (local_file,remote_file)
for name in dirs:
local_path = os.path.join(root,name)
a = local_path.replace(local_dir,'')
remote_path = os.path.join(remote_dir,a)
try:
sftp.mkdir(remote_path)
print "mkdir path %s" % remote_path
except Exception,e:
print e
print 'upload file success %s ' % datetime.datetime.now()
t.close()
except Exception,e:
print e
if __name__=='__main__':
local_dir='/home/soft/'
remote_dir='/tmp/aaa/'
upload(local_dir,remote_dir)
相关文章推荐
- learn python the hard way(笨办法学python) 练习39
- python实现识别相似图片小结
- python 模块
- python实现识别相似图片小结
- python实现搜索本地文件信息写入文件的方法
- python文件操作相关知识点总结整理
- python文件与目录操作实例详解
- python开发之str.format()用法实例分析
- python开发之list操作实例分析
- python 3 pyenv版本管理与pymsql、paramiko模块
- 【福利】Python入门基础知识大盘点
- spark python编程
- 利用python求解八数码难题
- Python-练习 洗牌
- 《笨办法学Python》 第42课手记
- python pygame安装与使用问题
- python基础学习,基础语法总结
- 已知线段端点坐标,求线段上等距离的点坐标序列
- 已知四边形四个顶点坐标,求面积
- 已知两点经纬度坐标,求其直线距离