python ftp 按目录结构上传下载的实现代码

yipeiwu_com5年前Python基础

具体代码如下所示:

#!/usr/bin/python
# coding=utf-8
from ftplib import FTP
import time
import os
def __ftp_upload(ftp,local,remote,isDel=False):
  if os.path.isdir(local):
    for f in os.listdir(local):
      if os.path.isdir(local+f):
        try:
          ftp.cwd(remote+f)
        except:
          ftp.mkd(remote+f)
        print local+f
        __ftp_upload(ftp,local+f+'/',remote+f+'/',isDel)
      else:
        print remote+f
        print local+f
        fp = open(local+f, 'rb')
        ftp.storbinary('STOR ' + remote + f, fp, 4096)
        fp.close()
        if (isDel==True):
          os.remove(local)
  else:
    fp = open(local+f, 'rb')
    ftp.storbinary('STOR ' + remote + f, fp, 4096)
    fp.close()
    if (isDel==True):
      os.remove(local)
def ftp_upload(host,port,username,password,local,remote,isDel=False):
  ftp = FTP()
  try:
    ftp.connect(host,port)
    ftp.login(username,password)
  except:
    return False
  try:
    __ftp_upload(ftp,local,remote,False)
  except Exception,e:
    print e
  ftp.close()
  return True
def ftp_download(host,port,username,password,local,remote):
  ftp = FTP()
  ftp.connect(host,port)
  ftp.login(username,password)
  ret = False
  try:
    if os.path.isdir(local):
      for f in ftp.dir(remote):
        fp = open(local+f, 'wb')
        ftp.retrbinary('RETR ' + remote + f, fp.write, 4096)
        fp.close()
    else:
      fp = open(local, 'wb')
      ftp.retrbinary('RETR ' + remote, fp.write, 4096)
      fp.close()
    ret = True
  except Exception,e:
    print ("download exception:\n",e)
  ftp.close()
  return ret
if __name__=='__main__':
  host = '*.*.*.*'
  port = '21'
  username = 'xxx'
  password = 'xxx'
  ftp_upload(host,port,username,password,'/home/pi/work/xx/','/home/ubuntu/xx/',False)
  print 'download'
  ftp_download(host,port,username,password,'/home/pi/work/xx/hh.txt','/home/ubuntu/xx/hh.txt')

只完成了按目录结构上传,下载还没弄好。

补充:下面看下Python ftp 上传和下载

工具

python3
ftplib

上传

from ftplib import FTP
ftp = FTP(host='127.0.0.1', user='test', passwd='test') #创建
ftp.cwd('/home/test/ftp/') #上传路径
fd = open('test.txt', 'rb') #以只读的方式打开要上传的文件
ftp.storbinary('STOR test.txt', fd) #上传文件
fd.close()
ftp.quit() #退出登录
ftp.close() #关闭连接

下载

from ftplib import FTP
ftp = FTP(host='127.0.0.1', user='test', passwd='test') #创建
ftp.cwd('/home/test/ftp/') #服务器下载路径
fd = open('test.txt', 'wb') #以只写的方式打开要下载的文件
ftp.retrbinary('RETR test.txt', fd.write, 2048) #下载文件
fd.close()
ftp.quit() #退出登录
ftp.close() #关闭连接

总结

以上所述是小编给大家介绍的jpython ftp 按目录结构上传下载的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

对python 读取线的shp文件实例详解

如下所示: import shapefile sf = shapefile.Reader("E:\\1.2\\cs\\DX_CSL.shp") shapes = sf.shapes(...

Python多层嵌套list的递归处理方法(推荐)

问题:用Python处理一个多层嵌套list ['and', 'B', ['not', 'A'],[1,2,1,[2,1],[1,1,[2,2,1]]], ['not', 'A',...

python执行外部程序的常用方法小结

本文实例总结了python执行外部程序的常用方法。分享给大家供大家参考。具体分析如下: 在python中我们可以通过下面的方法直接调用系统命令或者外部程序,使用方便 1、os模块的exe...

Python+OpenCV人脸检测原理及示例详解

Python+OpenCV人脸检测原理及示例详解

关于opencv OpenCV 是 Intel 开源计算机视觉库 (Computer Version) 。它由一系列 C 函数和少量 C++ 类构成,实现了图像处理和计算机视觉方面的很...

对Python中class和instance以及self的用法详解

一. Python 的类和实例 在面向对象中,最重要的概念就是类(class)和实例(instance),类是抽象的模板,而实例是根据类创建出来的一个个具体的 “对象”。 就好比,学生是...