python之FTP上传和下载

时间:2017-09-16 10:20:39   收藏:0   阅读:298
 1 # FTP操作
 2 import ftplib
 3 
 4 host = 192.168.20.191
 5 username = ftpuser
 6 password = ftp123
 7 file = 1.txt
 8 
 9 f = ftplib.FTP(host)  # 实例化FTP对象
10 f.login(username, password)  # 登录
11 
12 # 获取当前路径
13 pwd_path = f.pwd()
14 print("FTP当前路径:", pwd_path)
15 
16 
17 # 逐行读取ftp文本文件
18 # f.retrlines(‘RETR %s‘ % file)
19 
20 def ftp_download():
21     ‘‘‘以二进制形式下载文件‘‘‘
22     file_remote = 1.txt
23     file_local = D:\\test_data\\ftp_download.txt
24     bufsize = 1024  # 设置缓冲器大小
25     fp = open(file_local, wb)
26     f.retrbinary(RETR %s % file_remote, fp.write, bufsize)
27     fp.close()
28 
29 
30 def ftp_upload():
31     ‘‘‘以二进制形式上传文件‘‘‘
32     file_remote = ftp_upload.txt
33     file_local = D:\\test_data\\ftp_upload.txt
34     bufsize = 1024  # 设置缓冲器大小
35     fp = open(file_local, rb)
36     f.storbinary(STOR  + file_remote, fp, bufsize)
37     fp.close()
38 
39 
40 ftp_download()
41 ftp_upload()
42 f.quit()
43 

 

 FTP对象方法说明

 

原文:http://www.cnblogs.com/gongxr/p/7529949.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!