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

作者:liudijiang 时间:2021-01-28 00:38:33 

具体代码如下所示:


#!/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 按目录结构上传下载的实现代码网站的支持!

来源:https://blog.csdn.net/liudijiang/article/details/82655957

标签:python,目录结构,上传,下载
0
投稿

猜你喜欢

  • python函数运行内存时间等性能检测工具

    2021-04-08 02:24:00
  • python pandas中索引函数loc和iloc的区别分析

    2021-08-31 21:44:21
  • 用Python实现石头剪刀布游戏

    2023-06-05 05:37:41
  • 详解PHP设计模式之桥接模式

    2023-05-30 10:29:02
  • Go语言通过Luhn算法验证信用卡卡号是否有效的方法

    2023-07-23 17:28:07
  • 机器深度学习二分类电影的情感问题

    2022-07-17 06:13:28
  • 人脸识别实战之Opencv+SVM实现人脸识别

    2021-01-06 09:32:13
  • python中hashlib模块用法示例

    2023-03-20 12:20:13
  • python使用rstrip函数删除字符串末位字符

    2023-06-10 16:59:56
  • python实现机械分词之逆向最大匹配算法代码示例

    2022-01-02 08:16:27
  • python解决OpenCV在读取显示图片的时候闪退的问题

    2022-04-16 16:29:48
  • 网页中插入视频播放代码全集

    2007-10-22 17:48:00
  • 发布网站改版时的3要3不要

    2008-12-31 18:48:00
  • python如何提取英语pdf内容并翻译

    2023-06-13 13:37:27
  • Django项目搭建之实现简单的API访问

    2021-05-20 23:21:33
  • 浅谈python输出列表元素的所有排列形式

    2023-02-10 11:09:39
  • 实例讲解Python脚本成为Windows中运行的exe文件

    2023-07-15 02:14:31
  • 状态机的概念和在Python下使用状态机的教程

    2023-03-24 14:06:11
  • Windows下安装python MySQLdb遇到的问题及解决方法

    2022-07-20 13:22:36
  • 利用python实现汉字转拼音的2种方法

    2023-12-08 09:58:50
  • asp之家 网络编程 m.aspxhome.com