Below for you to share a Python based FTP module FTP file Upload, has a good reference value, I hope to be helpful to everyone. Come and see it together.
The example in this paper describes the ftp file upload operation based on the FTP module of Python. Share to everyone for your reference, as follows:
#!/usr/bin/python#-*-coding:utf-8-*-from ftplib import FTP #加载ftp模块ftp =ftp () #设置变量ftp. Set_debuglevel (2) #打开调试级别2, Show Details ftp.connect ("IP", "Port") #连接的ftp Sever and Port Ftp.login ("User", "password") #连接的用户名, password print Ftp.getwelcome () #打印出欢迎信息ftp. CWD ("xxx/xxx") #更改远程目录bufsize =1024 #设置的缓冲区大小filename = "Filename.txt" # Files that need to be downloaded file_handle=open (filename, "WB"). write# Open the file locally in write mode Ftp.retrbinaly ("RETR filename.txt", file_handle,bufsize) #接收服务器上文件并写入本地文件ftp. Set_debuglevel (0) #关闭调试模式ftp. Quit #退出ftpftp. 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) #上传目标文件
A simple example came from
#!/usr/bin/python#coding=utf-8from ftplib Import ftp #引入ftp模块import osftp = FTP ("IP") #设置ftp服务器地址ftp. Login (' Username ', ' password ') #设置登录账户和密码ftp. Retrlines (' LIST ') #列出文件目录ftp. CWD (' a ') #选择操作目录ftp. Retrlines (' LIST ') #列出目录文件localfile = '/mnt/nasfile/ftp test/new feature. 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-8import osfrom ftplib import ftpdef ftpconnect (host, username, password): FTP = FTP () #ftp. Set_debuglevel (2 ) #打开调试级别2, Show details Ftp.connect (host, Ftp.login) #连接 (username, password) #登录, if anonymous login is replaced with an empty string to return ftpdef down LoadFile (FTP, RemotePath, localpath): bufsize = 1024x768 #设置缓冲块大小 ftp.cwd (' Micro-agricultural credit ') FP = open (LocalPath, ' WB ') #以写模式在本 To open the file ftp.retrbinary (' RETR ' + RemotePath, Fp.write, bufsize) #接收服务器上文件并写入本地文件 ftp.set_debuglevel (0) #关闭调试 Fp.close () #关闭文件def UploadFile (FTP, RemotePath, localpath): BufSize = 1024x768 ftp.cwd (' Micro-agricultural credit ') FP = open (LocalPath, ' RB ') Ftp.storbinary (' STOR ' + remotepath, FP, bufsize) #上传文件 ftp.set_debuglevel (0) Fp.close () # Use the OS module walk function to search for all Excel text in a directory Pieces ##################### #获取同一个文件夹下的所有excel文件名 ###################### #def getfilename (filepath): File_list = [] for root , dirs, files in Os.walk (filepath): Filespath in Files: # print (Os.path.join (root, Filespath)) file_list. Append (Os.path.join (Root, filEspath)) return file_listif __name__ = = "__main__": FTP = ftpconnect ("IP", "account", "password") ######## #设置本地读取文件路径 ############# # filepath= ' c:/pic/data/' file_list = GetFileName (filepath) print len (file_list) for each of file_list:print each Localfile=each remotepath=os.path.basename (LocalFile) UploadFile (FTP, RemotePath, LocalFile) ftp.quit ()