xywuyiba6 发表于 2018-8-10 11:40:42

python通过sftp远程传输文件

import os  def put(sftp,local,remote):
  #检查路径是否存在
  def _is_exists(path,function):
  path = path.replace('\\','/')
  try:
  function(path)
  except Exception as error:
  return False
  else:
  return True
  #拷贝文件
  def _copy(sftp,local,remote):
  #判断remote是否是目录
  if _is_exists(remote,function=sftp.chdir):
  #是,获取local路径中的最后一个文件名拼接到remote中
  filename = os.path.basename(os.path.normpath(local))
  remote = os.path.join(remote,filename).replace('\\','/')
  #如果local为目录
  if os.path.isdir(local):
  #在远程创建相应的目录
  _is_exists(remote,function=sftp.mkdir)
  #遍历local
  for file in os.listdir(local):
  #取得file的全路径
  localfile = os.path.join(local,file).replace('\\','/')
  #深度递归_copy()
  _copy(sftp=sftp,local=localfile,remote=remote)
  #如果local为文件
  if os.path.isfile(local):
  try:
  sftp.put(local,remote)
  except Exception as error:
  print error
  print '',local,'==>',remote,'FAILED'
  else:
  print '',local,'==>',remote,'SUCCESSED'
  #检查local
  if not _is_exists(local,function=os.stat):
  print "'"+local+"': No such file or directory in local"
  return False
  #检查remote的父目录
  remote_parent =os.path.dirname(os.path.normpath(remote))
  if not _is_exists(remote_parent,function=sftp.chdir):
  print "'"+remote+"': No such file or directory in remote"
  return False
  #拷贝文件
  _copy(sftp=sftp,local=local,remote=remote)
页: [1]
查看完整版本: python通过sftp远程传输文件