Home  >  Article  >  Backend Development  >  Python implements ftp file upload based on FTP module

Python implements ftp file upload based on FTP module

不言
不言Original
2018-04-23 11:57:312099browse

Below I will share with you an article about Python implementing ftp file upload based on the FTP module. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

The example in this article describes how Python implements ftp file upload operations based on the FTP module. Share it with everyone for your reference, the details are as follows:


#!/usr/bin/python
#-*- coding:utf-8 -*-
from ftplib import FTP  #加载ftp模块
ftp=FTP()     #设置变量
ftp.set_debuglevel(2)  #打开调试级别2,显示详细信息
ftp.connect("IP","port") #连接的ftp sever和端口
ftp.login("user","password")#连接的用户名,密码
print ftp.getwelcome()  #打印出欢迎信息
ftp.cwd("xxx/xxx")  #更改远程目录
bufsize=1024    #设置的缓冲区大小
filename="filename.txt" #需要下载的文件
file_handle=open(filename,"wb").write
#以写模式在本地打开文件
ftp.retrbinaly("RETR filename.txt",file_handle,bufsize)
#接收服务器上文件并写入本地文件
ftp.set_debuglevel(0)  #关闭调试模式
ftp.quit     #退出ftp
ftp.dir()     #显示目录下文件信息
ftp.mkd(pathname)   #新建远程目录
ftp.pwd()     #返回当前所在位置
ftp.rmd(dirname)   #删除远程目录
ftp.delete(filename)  #删除远程文件
ftp.rename(fromname, toname)#将fromname修改名称为toname。
ftp.storbinaly("STOR filename.txt",file_handel,bufsize)#上传目标文件


Upload a simple example


#!/usr/bin/python
#coding=utf-8
from ftplib import FTP         #引入ftp模块
import os
ftp = FTP("ip")           #设置ftp服务器地址
ftp.login('username', 'password')      #设置登录账户和密码
ftp.retrlines('LIST')          #列出文件目录
ftp.cwd('a')            #选择操作目录
ftp.retrlines('LIST')          #列出目录文件
localfile = '/mnt/NasFile/ftp测试/新功能.doc'    #设定文件位置
f = open(localfile, 'rb')        #打开文件
#file_name=os.path.split(localfile)[-1]
#ftp.storbinary('STOR %s'%file_name, f , 8192)
ftp.storbinary('STOR %s' % os.path.basename(localfile), f) #上传文件


Full version:


#coding: utf-8
import os
from ftplib import FTP
def ftpconnect(host, username, password):
  ftp = FTP()
  #ftp.set_debuglevel(2)     #打开调试级别2,显示详细信息
  ftp.connect(host, 21)     #连接
  ftp.login(username, password) #登录,如果匿名登录则用空串代替即可
  return ftp
def downloadfile(ftp, remotepath, localpath):
  bufsize = 1024        #设置缓冲块大小
  ftp.cwd('微农贷')
  fp = open(localpath,'wb')   #以写模式在本地打开文件
  ftp.retrbinary('RETR ' + remotepath, fp.write, bufsize) #接收服务器上文件并写入本地文件
  ftp.set_debuglevel(0)     #关闭调试
  fp.close()          #关闭文件
def uploadfile(ftp, remotepath, localpath):
  bufsize = 1024
  ftp.cwd('微农贷')
  fp = open(localpath, 'rb')
  ftp.storbinary('STOR '+ remotepath , fp, bufsize) #上传文件
  ftp.set_debuglevel(0)
  fp.close()
# 使用os模块walk函数,搜索出某目录下的全部excel文件
######################获取同一个文件夹下的所有excel文件名#######################
def getFileName(filepath):
  file_list = []
  for root, dirs, files in os.walk(filepath):
    for filespath in files:
      # print(os.path.join(root, filespath))
      file_list.append(os.path.join(root, filespath))
  return file_list
if __name__ == "__main__":
  ftp = ftpconnect("ip", "账号", "密码")
  #########设置本地读取文件路径##############
  filepath='C:/pic/data/'
  file_list = getFileName(filepath)
  print len(file_list)
  for each in file_list:
    print each
    localfile=each
    remotepath=os.path.basename(localfile)
    uploadfile(ftp, remotepath, localfile)
  ftp.quit()

##Related recommendations:

Python example of text recognition based on Baidu AI


The above is the detailed content of Python implements ftp file upload based on FTP module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn