python ftp 上传、下载文件 #获取昨天日期 TODAY = datetime.date.today() YESTERDAY = TODAY - datetime.timedelta(days=1)CURRENTDAY=YESTERDAY.strftime(&#39;%Y%m%d&#39;) --------------------------------------------------------------------------------------- #!/usr/bin/env python # -*- coding: cp936 -*- #导入ftplib扩展库 import ftplib #创建ftp对象实例 ftp = ftplib.FTP() #指定IP地址和端口,连接到FTP服务,上面显示的是FTP服务器的Welcome信息 FTPIP= "218.108.***.***" FTPPORT= 21 USERNAME= "ybmftp" USERPWD= "ybm***" ftp.connect(FTPIP,FTPPORT) #通过账号和密码登录FTP服务器 ftp.login(USERNAME,USERPWD) #如果参数 pasv 为真,打开被动模式传输 (PASV MODE) , #否则,如果参数 pasv 为假则关闭被动传输模式。 #在被动模式打开的情况下,数据的传送由客户机启动,而不是由服务器开始。 #这里要根据不同的服务器配置 ftp.set_pasv(0) #在FTP连接中切换当前目录 CURRTPATH= "/home1/ftproot/ybmftp/testupg/payment" ftp.cwd(CURRTPATH) #为准备下载到本地的文件,创建文件对象 DownLocalFilename="YBM_20110629_9001_CHK" f = open(DownLocalFilename,&#39;wb&#39;) #从FTP服务器下载文件到前一步创建的文件对象,其中写对象为f.write,1024是缓冲区大小 DownRoteFilename="YBM_20110629_9001_CHK" ftp.retrbinary(&#39;RETR &#39; + DownRoteFilename , f.write ,1024) #关闭下载到本地的文件 #提醒:虽然Python可以自动关闭文件,但实践证明,如果想下载完后立即读该文件,最好关闭后重新打开一次 f.close() #关闭FTP客户端连接 ftp.close() ###上传文件 #! /usr/bin/env python from ftplib import FTP import sys, getpass, os.path host="218.108.***.***" username="ybmftp" password="ybm!***" localfile="/home/gws/xym/script/duizhang.txt" remotepath="~/testpayment" f=FTP(host) f.login(username, password) f.cwd(remotepath) fd=open(localfile,&#39;rb&#39;)print os.path.basename(localfile) #否则,如果参数 pasv 为假则关闭被动传输模式。 #在被动模式打开的情况下,数据的传送由客户机启动,而不是由服务器开始。 #这里要根据不同的服务器配置 ftp.set_pasv(0) f.storbinary(&#39;STOR %s &#39; % os.path.basename(localfile),fd) fd.close() f.quit Python中的ftplib模块 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件 FTP的工作流程及基本操作可参考协议RFC959 ftp登陆连接 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.cmd("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相关命令操作 ftp.cwd(pathname) #设置FTP当前操作的路径 ftp.dir() #显示目录下文件信息 ftp.nlst() #获取目录下的文件 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) #上传目标文件 ftp.retrbinary("RETR filename.txt",file_handel,bufsize)#下载FTP文件 from ftplib import FTP ftp = FTP() timeout = 30 port = 21 ftp.connect(&#39;192.168.1.188&#39;,port,timeout) # 连接FTP服务器 ftp.login(&#39;UserName&#39;,&#39;888888&#39;) # 登录 print ftp.getwelcome() # 获得欢迎信息 ftp.cwd(&#39;file/test&#39;) # 设置FTP路径 list = ftp.nlst() # 获得目录列表 for name in list: print(name) # 打印文件名字 path = &#39;d:/data/&#39; + name # 文件保存路径 f = open(path,&#39;wb&#39;) # 打开要保存文件 filename = &#39;RETR &#39; + name # 保存FTP文件 ftp.retrbinary(filename,f.write) # 保存FTP上的文件 ftp.delete(name) # 删除FTP文件 ftp.storbinary(&#39;STOR &#39;+filename, open(path, &#39;rb&#39;)) # 上传FTP文件 ftp.quit() # 退出FTP服务器 import ftplib import os import socket HOST = &#39;ftp.mozilla.org&#39; DIRN = &#39;pub/mozilla.org/webtools&#39; FILE = &#39;bugzilla-3.6.7.tar.gz&#39; def main(): try: f = ftplib.FTP(HOST) except (socket.error, socket.gaierror): print &#39;ERROR:cannot reach " %s"&#39; % HOST return print &#39;***Connected to host "%s"&#39; % HOST try: f.login() except ftplib.error_perm: print &#39;ERROR: cannot login anonymously&#39; f.quit() return print &#39;*** Logged in as "anonymously"&#39; try: f.cwd(DIRN) except ftplib.error_perm: print &#39;ERRORL cannot CD to "%s"&#39; % DIRN f.quit() return print &#39;*** Changed to "%s" folder&#39; % DIRN try: #传一个回调函数给retrbinary() 它在每接收一个二进制数据时都会被调用 f.retrbinary(&#39;RETR %s&#39; % FILE, open(FILE, &#39;wb&#39;).write) except ftplib.error_perm: print &#39;ERROR: cannot read file "%s"&#39; % FILE os.unlink(FILE) else: print &#39;*** Downloaded "%s" to CWD&#39; % FILE f.quit() return if name == &#39;main&#39;: main() os.listdir(dirname):列出dirname下的目录和文件 os.getcwd():获得当前工作目录 os.curdir:返回当前目录(&#39;.&#39;) os.chdir(dirname):改变工作目录到dirname os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false os.path.isfile(name):判断name是不是一个文件,不存在name也返回false os.path.exists(name):判断是否存在文件或目录name os.path.getsize(name):获得文件大小,如果name是目录返回0L os.path.abspath(name):获得绝对路径 os.path.normpath(path):规范path字符串形式 os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在) os.path.splitext():分离文件名与扩展名 os.path.join(path,name):连接目录与文件名或目录 os.path.basename(path):返回文件名 os.path.dirname(path):返回文件路径 os.remove(dir) #dir为要删除的文件夹或者文件路径 os.rmdir(path) #path要删除的目录的路径。需要说明的是,使用os.rmdir删除的目录必须为空目录,否则函数出错。 os.path.getmtime(name) #获取文件的修改时间 os.stat(path).st_mtime#获取文件的修改时间 os.stat(path).st_ctime #获取文件修改时间 os.path.getctime(name)#获取文件的创建时间 python中对文件、文件夹的操作需要涉及到os模块和shutil模块。 创建文件: 1) os.mknod("test.txt") 创建空文件 2) open("test.txt",w) 直接打开一个文件,如果文件不存在则创建文件 创建目录: os.mkdir("file") 创建目录 复制文件: shutil.copyfile("oldfile","newfile") oldfile和newfile都只能是文件 shutil.copy("oldfile","newfile") oldfile只能是文件夹,newfile可以是文件,也可以是目标目录 复制文件夹: shutil.copytree("olddir","newdir") olddir和newdir都只能是目录,且newdir必须不存在 重命名文件(目录) os.rename("oldname","newname") 文件或目录都是使用这条命令 移动文件(目录) shutil.move("oldpos","newpos") 删除文件 os.remove("file") 删除目录 os.rmdir("dir") 只能删除空目录 shutil.rmtree("dir") 空目录、有内容的目录都可以删 转换目录 os.chdir("path") 换路径 判断目标 os.path.exists("goal") 判断目标是否存在 os.path.isdir("goal") 判断目标是否目录 os.path.isfile("goal") 判断目标是否文件 Python 实现文件复制、删除 import os import shutil filelist=[] rootdir="/home/zoer/aaa" filelist=os.listdir(rootdir) for f in filelist: filepath = os.path.join( rootdir, f ) if os.path.isfile(filepath): os.remove(filepath) print filepath+" removed!" elif os.path.isdir(filepath): shutil.rmtree(filepath,True) print "dir "+filepath+" removed!" 用python实现了一个小型的自动发版本的工具。这个“自动发版本”有点虚, 只是简单地把debug 目录下的配置文件复制到指定目录,把Release下的生成文件复制到同一指定,过滤掉不需要的文件夹(.svn),然后再往这个指定目录添加几个特定的 文件。 这个是我的第一个python小程序。 下面就来看其代码的实现。 首先插入必要的库: import os import os.path import shutil import time, datetime 然后就是一大堆功能函数。第一个就是把某一目录下的所有文件复制到指定目录中: def copyFiles(sourceDir, targetDir): if sourceDir.find(".svn") > 0: return for file in os.listdir(sourceDir): sourceFile = os.path.join(sourceDir, file) targetFile = os.path.join(targetDir, file) if os.path.isfile(sourceFile): if not os.path.exists(targetDir): os.makedirs(targetDir) if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))): open(targetFile, "wb").write(open(sourceFile, "rb").read()) if os.path.isdir(sourceFile): First_Directory = False copyFiles(sourceFile, targetFile) 删除一级目录下的所有文件: def removeFileInFirstDir(targetDir): for file in os.listdir(targetDir): targetFile = os.path.join(targetDir, file) if os.path.isfile(targetFile): os.remove(targetFile) 复制一级目录下的所有文件到指定目录: def coverFiles(sourceDir, targetDir): for file in os.listdir(sourceDir): sourceFile = os.path.join(sourceDir, file) targetFile = os.path.join(targetDir, file) #cover the files if os.path.isfile(sourceFile): open(targetFile, "wb").write(open(sourceFile, "rb").read()) 复制指定文件到目录: def moveFileto(sourceDir, targetDir): shutil.copy(sourceDir, targetDir) 往指定目录写文本文件: def writeVersionInfo(targetDir): open(targetDir, "wb").write("Revison:") 返回当前的日期,以便在创建指定目录的时候用: def getCurTime(): nowTime = time.localtime() year = str(nowTime.tm_year) mOnth= str(nowTime.tm_mon) if len(month) <2: mOnth= &#39;0&#39; + month day = str(nowTime.tm_yday) if len(day) <2: day = &#39;0&#39; + day return (year + &#39;-&#39; + month + &#39;-&#39; + day) 然后就是主函数的实现了: if name =="main": print "Start(S) or Quilt(Q) \n" flag = True while (flag): answer = raw_input() if &#39;Q&#39; == answer: flag = False elif &#39;S&#39;== answer : formatTime = getCurTime() targetFoldername = "Build " + formatTime + "-01" Target_File_Path += targetFoldername copyFiles(Debug_File_Path, Target_File_Path) removeFileInFirstDir(Target_File_Path) coverFiles(Release_File_Path, Target_File_Path) moveFileto(Firebird_File_Path, Target_File_Path) moveFileto(AssistantGui_File_Path, Target_File_Path) writeVersionInfo(Target_File_Path+"\\ReadMe.txt") print "all sucess" else: print "not the correct command"linux下python脚本判断目录和文件是否存在 if os.path.isdir(&#39;E:test&#39;): pass else: os.mkdir(&#39;E:test&#39;) ##os.mkdir() 只会创建一个目录,不可以级联创建 eg2: if not os.path.exists(&#39;E:test&#39;): ###判断文件是否存在,返回布尔值 os.makedirs(&#39;E:test&#39;) ##os.makedirs() 这个连同中间的目录都会创建,类似于参数mkdir -p eg3: try: fp = open("file_path") catch exception: except 和catch的区别? os.mkdir(&#39;file_path&#39;) ##os.mkdir() 只会创建一个目录,不可级联创建,但要有异常处理的意识 fp = open("file_path" eg4:实测 #!/usr/bin/env python import os FILE_PATH=&#39;/home/wuxy/aaa111/222/333/444.txt&#39; ###444.txt 不会当做文件,而是当做目录 if os.path.exists(&#39;FILE_PATH&#39;): ##目录存在,返回为真 print &#39;dir not exists&#39; os.makedirs(FILE_PATH) ###FILE_PATH不用加引号。否则会报错 else: print &#39;dir exists&#39; python实现ftp上传下载文件 #!/usr/bin/env python # encoding: utf-8 author = "pwy" &#39;&#39;&#39; 上传:上传文件并备份到其他目录 下载:下载文件,并删除远端文件 &#39;&#39;&#39; from ftplib import FTP from time import sleep import os,datetime,logging from shutil import move HOST = "192.168.1.221" USER = "sxit" PASSWORD = "1qaz!QAZ" #PORT = "" #Upload the file, change the directory remotedir = "/home/test/" localdir = "/home/sxit/object/" bakdir = "/home/sxit/bak" #Download the file, change the directory Remoredir = "/home/sxit/object1/" Localdir = "/root/ftp-logging" LOGFILE = datetime.datetime.now().strftime(&#39;%Y-%m-%d&#39;)+&#39;.log&#39; logging.basicConfig(level=logging.INFO, format=&#39;%(asctime)s %(filename)s %(levelname)s %(message)s&#39;, # datefmt=&#39;%a, %d %b %Y %H:%M:%S&#39;, filename= LOGFILE, filemode=&#39;a&#39;) logging.FileHandler(LOGFILE) class CLASS_FTP: def init(self,HOST,USER,PASSWORD,PORT=&#39;21&#39;): self.HOST = HOST self.USER = USER self.PASSWORD = PASSWORD self.PORT = PORT self.ftp=FTP() self.flag=0 # 0:no connected, 1: connting def Connect(self): try: if self.flag == 1: logging.info("ftp Has been connected") else: self.ftp.connect(self.HOST,self.PORT) self.ftp.login(self.USER,self.PASSWORD) # self.ftp.set_pasv(False) self.ftp.set_debuglevel(0) self.flag=1 except Exception: logging.info("FTP login failed") def Up_load(self,remotedir,localdir,bakdir): try: self.ftp.cwd(remotedir) for i in os.listdir(localdir): if i.endswith(&#39;.txt&#39;): file_handler = open(i,&#39;rb&#39;) self.ftp.storbinary(&#39;STOR %s&#39; % i,file_handler) logging.info("%s already upload ."%i) try: if os.path.isdir(bakdir): move(i,bakdir) logging.info("%s move to %s ." % (i,bakdir)) else: print "Move the file FAILED" logging.info("Move the %s to %s FAILED!"%(i,bakdir)) except Exception: logging.info("ftp delete file faild !!!!!") file_handler.close() # self.ftp.quit() except Exception: logging.info("Up_load failed") def Down_load(self,Remoredir,Localdir): try: self.ftp.cwd(Remoredir) for i in self.ftp.nlst(): if i.endswith(&#39;.NET&#39;): #match file file_handler = open(i,&#39;wb&#39;) self.ftp.retrbinary(&#39;RETR %s&#39; % i,file_handler.write) logging.info("%s already down ."%i) try: self.ftp.delete(i) logging.info("%s already deleted!"%i) except Exception: logging.info("ftp delete file faild !!!!!") file_handler.close() #self.ftp.quit() except Exception: logging.info("Down_load failed") if name == &#39;main&#39;: ftp = CLASS_FTP(HOST,USER,PASSWORD) while True: ftp.Connect() # ftp.Down_load(Remoredir,Localdir) ftp.Up_load(remotedir,localdir,bakdir) sleep(30)常用函数用手册查看,以下只是简略,因为没用用到,[待整理]: login(user=&#39;&#39;,passwd=&#39;&#39;, acct=&#39;&#39;) 登录到FTP 服务器,所有的参数都是可选的 pwd() 当前工作目录 cwd(path) 把当前工作目录设置为path dir([path[,...[,cb]]) 显示path 目录里的内容,可选的参数cb 是一个回调函数,会被传给retrlines()方法 nlst([path[,...]) 与dir()类似,但返回一个文件名的列表,而不是显示这些文件名 retrlines(cmd [, cb]) 给定FTP 命令(如“RETR filename”),用于下载文本文件。可选的回调函数cb 用于处理文件的每一行 retrbinary(cmd, cb[,bs=8192[, ra]]) 与retrlines()类似,只是这个指令处理二进制文件。回调函数cb 用于处理每一块(块大小默认为8K)下载的数据。 storlines(cmd, f) 给定FTP 命令(如“STOR filename”),以上传文本文件。要给定一个文件对象f storbinary(cmd, f[,bs=8192]) 与storlines()类似,只是这个指令处理二进制文件。要给定一个文件对象f,上传块大小bs 默认为8Kbs=8192]) rename(old, new) 把远程文件old 改名为new delete(path) 删除位于path 的远程文件 mkd(directory) 创建远程目录 ftp &#39;&#39;&#39;第一个例子&#39;&#39;&#39; def get_C(self,target_dir=None): C = [] print "PWD:", self.ftp.pwd() if target_dir is not None: self.ftp.cwd(target_dir)# change working directory to target_dir server_file_list = [] fuck_callback = lambda x: (server_file_list.append(x)) self.ftp.retrlines(&#39;LIST&#39;, fuck_callback) # print server_file_list server_file_items = self.filter_dir_list(server_file_list) for item in server_file_items: if item.is_dir: print &#39;name = &#39;, item.name sub_C = self.get_C(item.name) # sub_C = [&#39;/&#39;+item.name+&#39;/&#39;+cc.name for cc in sub_C] for cc in sub_C: cc.name = &#39;/&#39; + item.name + cc.name print &#39;name --- &#39;,cc.name C.extend(sub_C) else: item.name = &#39;/&#39; + item.name C.append(item) self.ftp.cwd(&#39;..&#39;) return C def runtest(self,next_dir): C = ftp.get_C(next_dir) next_dir2=next_dir[2:] C = [cc.pack for cc in C] for i in C: print i next_dir1=i pos=next_dir1.rindex(&#39;/&#39;) next_dir3= next_dir1[0:pos] all_path=next_dir2 + next_dir3 print all_path next_dir_local = all_path.decode(&#39;utf8&#39;).encode(&#39;gbk&#39;) try: print next_dir_local #os.makedirs(next_dir_local) except OSError: pass #os.chdir(next_dir_local) localfile=next_dir1[pos+1:] print localfile allall_path=all_path + "/" + localfile self.ftp.cwd(&#39;/&#39;) print self.ftp.pwd() #file_handler = open(localfile, &#39;wb&#39;) #self.ftp.retrbinary(&#39;RETR %s&#39; % (allall_path), file_handler.write) #file_handler.close() &#39;&#39;&#39;第一个例子获取成/home/user/test.txt这样的列表&#39;&#39;&#39; 第二个例子 def download_files(self, localdir=&#39;./&#39;, remotedir=&#39;./&#39;): try: self.ftp.cwd(remotedir) except: debug_print(&#39;目录%s不存在,继续...&#39; % remotedir) return if not os.path.isdir(localdir): pass #os.makedirs(localdir) debug_print(&#39;切换至目录 %s&#39; % self.ftp.pwd()) self.file_list = [] self.ftp.dir(self.get_file_list) remotenames = self.file_list print(remotenames) # return for item in remotenames: filetype = item[0] filename = item[1] print "filename:",filename local = os.path.join(localdir, filename) if filetype == &#39;d&#39;: self.download_files(local, filename) elif filetype == &#39;-&#39;: self.download_file(local, filename) self.ftp.cwd(&#39;..&#39;) debug_print(&#39;返回上层目录 %s&#39; % self.ftp.pwd()) f.download_files(rootdir_local, rootdir_remote) &#39;&#39;&#39;第二个例子&#39;&#39;&#39; sftp s_file = path.join(path_name,name).replace(&#39;\\&#39;,&#39;/&#39;) def process_sftp_dir(path_name): """ 此函数递归处理sftp server端的目录和文件,并在client端创建所有不存在的目录,然后针对每个文件在两端的全路径执行get操作. path_name第一次的引用值应该是source_path的值 """ d_path = path_name.replace(source_path,destination_path,1) if not path.exists(d_path): # 若目标目录不存在则创建 print(&#39;%s----Create Local Dir: %s&#39; % (&#39; &#39;*8,d_path)) try: makedirs(d_path) # 递归创建不存在的目录 except Exception as err: print(&#39;%s----Create %s Failed&#39; % (&#39; &#39;*8,d_path)) print(&#39;{}----{}&#39;.format(&#39; &#39;*8,err)) exit(10) for name in (i for i in sftp.listdir(path=path_name) if not i.startswith(&#39;.&#39;)): """去掉以.开头的文件或目录""" s_file = path.join(path_name,name).replace(&#39;\\&#39;,&#39;/&#39;) # 在win环境下组合路径所用的&#39;\\&#39;换成&#39;/&#39; d_file = s_file.replace(source_path,destination_path,1) # 目标端全路径 chk_r_path_result = check_remote_path(s_file) if chk_r_path_result == &#39;file&#39;: # 文件 sftp_get(s_file,d_file,12) elif chk_r_path_result == &#39;directory&#39;: # 目录 process_sftp_dir(s_file) # 递归调用本身 process_sftp_dir(source_path) 区别很大 ftp: ftp.retrlines(&#39;LIST&#39;, fuck_callback) 完全是循环,目录的进行循环操作,而文件下载。最底层目录的文件下载完,回归上级目录。继续循环。 self.ftp.pwd() self.ftp.dir(self.get_file_list) get_file_list(self, line) self.ftp.cwd(&#39;..&#39;) self.ftp.cwd(remotedir) self.download_file(local, filename) 建立好本地目录,然后cd到远程目录,下载 sftp: sftp.listdir s_file = path.join(path_name,name).replace(&#39;\\&#39;,&#39;/&#39;) 指定源全路径下载 代码格式乱了,详细例子 ftp 第一个例子 # !/usr/bin/env python # -*-coding:utf-8-*- from ftplib import FTP from time import sleep import os, datetime,logging,time import string,re d1 = datetime.datetime.now() &#39;&#39;&#39;mOnths=[&#39;Jan&#39;,&#39;Feb&#39;,&#39;March&#39;,&#39;Apr&#39;,&#39;May&#39;,&#39;Jun&#39;,&#39;Jul&#39;,&#39;Aug&#39;,&#39;Sep&#39;] patternm = r&#39;2017.*|201611.*|201612.*|201610.*&#39; patternxml = r&#39;.*2016&#39; patternx = r&#39;xx.*&#39;&#39;&#39;&#39;&#39; HOST = "192.168.1.100" USER = "ftpuser3" PASSWORD = "test1passwd" class Myfile(object): def init(self, name, size, mtime): self.name = name # 文件名字 self.mtime = mtime # 文件创建时间 self.is_dir = False # 是否为文件夹,默认为不是文件夹 #self.size = float(size) / (1024 * 1024) # 文件大小 size = float(size) if size > 1024*1024: self.size = str(&#39;%.2f&#39;%(size / (1024*1024))) + &#39;MB&#39; elif size > 1024: self.size = str(&#39;%.2f&#39;%(size / 1024)) + &#39;KB&#39; else: self.size = str(size) + &#39;Bytes&#39; @property def is_file(self): return not self.is_dir @property def dir_property(self): if self.is_dir==True: return &#39;dir&#39; return &#39;file&#39; def show(self): print &#39;[%s], [%s], [%s], [%s]&#39; % (self.name, self.size, self.mtime, self.dir_property) @property def pack(self): """ 将myfile对象封装为一个字符串 :return: """ #return &#39;[%s][%s][%s]&#39;%(self.name, self.size, self.mtime) #return &#39;[%s][%s]&#39;%(self.name, self.size) return &#39;%s&#39; %(self.name) class CLASS_FTP: def init(self, HOST, USER, PASSWORD, PORT=&#39;21&#39;): self.HOST = HOST self.USER = USER self.PASSWORD = PASSWORD self.PORT = PORT self.ftp = FTP() self.flag = 0 # 0:no connected, 1: connting def Connect(self): try: if self.flag == 1: logging.info("ftp Has been connected") else: self.ftp.connect(self.HOST, self.PORT) self.ftp.login(self.USER, self.PASSWORD) # self.ftp.set_pasv(False) self.ftp.set_debuglevel(0) self.flag = 1 except Exception: logging.info("FTP login failed") def str_codec_std(self,mystr): return mystr.decode(&#39;utf8&#39;).encode(&#39;gbk&#39;) def dirmakedirs(self,next_dir_local,local_dir): # next_dir_local2= next_dir_local.split(&#39;/&#39;)[1:] next_dir_local2 = next_dir_local[1:].replace(&#39;/&#39;, &#39;\\&#39;) # next_dir_localw = next_dir_local2.decode(&#39;utf8&#39;).encode(&#39;gbk&#39;) # windows用这个 s_file = os.path.join(local_dir, next_dir_local2) print "s_file", s_file if not os.path.exists(s_file): try: os.makedirs(s_file) except OSError: pass os.chdir(s_file) def filter_dir_list(self,mystr_list): res = [] for mystr in mystr_list: #mystr = self.str_codec_std(mystr) # print "mystr is :%s" % mystr file_info = string.split(mystr, maxsplit=8) name = file_info[8] print &#39;name = &#39;, name if name == &#39;.&#39; or name == &#39;..&#39;: continue size = file_info[4] mtime = &#39;%s-%s-%s&#39; % (file_info[5], file_info[6], file_info[7]) myfile = Myfile(name=name, size=size, mtime=mtime) dir_info = file_info[0] if dir_info[0] == &#39;d&#39;: myfile.is_dir = True res.append(myfile) return res def get_C(self,target_dir=None,local_dir=None): C = [] if target_dir is not None: self.ftp.cwd(target_dir)# change working directory to target_dir server_file_list = [] fuck_callback = lambda x: (server_file_list.append(x)) self.ftp.retrlines(&#39;LIST&#39;, fuck_callback) next_dir_local = self.ftp.pwd() self.dirmakedirs(next_dir_local, local_dir) server_file_items = self.filter_dir_list(server_file_list) for item in server_file_items: if item.is_dir: sub_C = self.get_C(item.name,local_dir) for cc in sub_C: cc.name = &#39;/&#39; + item.name + cc.name C.extend(sub_C) else: item.name = &#39;/&#39; + item.name C.append(item) self.ftp.cwd(&#39;..&#39;) return C def runtest(self,local_dir,next_dir): os.chdir(local_dir) C = ftp.get_C(next_dir,local_dir) next_dir2=next_dir[2:] C = [cc.pack for cc in C] print "C:",C for i in C: next_dir1=i pos=next_dir1.rindex(&#39;/&#39;) next_dir3= next_dir1[0:pos] all_path=next_dir2 + next_dir3 self.dirmakedirs(all_path, local_dir) next_dir_localz = all_path[1:].replace(&#39;/&#39;, &#39;\\&#39;) &#39;&#39;&#39;# next_dir_local = next_dir_localz # next_dir_local = next_dir_localz.decode(&#39;utf8&#39;).encode(&#39;gbk&#39;) #windows用这个&#39;&#39;&#39; # s_file = os.path.join(local_dir, next_dir_localz) # try: # os.makedirs(s_file) # except OSError: # pass # os.chdir(s_file) localfile=next_dir1[pos+1:] print localfile allall_path=all_path + "/" + localfile file_handler = open(localfile, &#39;wb&#39;) self.ftp.retrbinary(&#39;RETR %s&#39; % (allall_path), file_handler.write) file_handler.close() if name == &#39;main&#39;: ftp = CLASS_FTP(HOST, USER, PASSWORD) ftp.Connect() ftp.runtest(&#39;D:\\ftp&#39;,&#39;./&#39;) d2 = datetime.datetime.now() print d2 - d1 &#39;&#39;&#39;参数乱七八糟&#39;&#39;&#39; ftp 第二个例子 别人2010写好的 # !/usr/bin/env python # coding:utf-8 from ftplib import FTP import os, sys, string, datetime, time import socket class MYFTP: def init(self, hostaddr, username, password, remotedir, port=21): self.hostaddr = hostaddr self.username = username self.password = password self.remotedir = remotedir self.port = port self.ftp = FTP() self.file_list = [] # self.ftp.set_debuglevel(2) def del(self): self.ftp.close() # self.ftp.set_debuglevel(0) def login(self): ftp = self.ftp try: timeout = 60 socket.setdefaulttimeout(timeout) ftp.set_pasv(True) print &#39;开始连接到 %s&#39; % (self.hostaddr) ftp.connect(self.hostaddr, self.port) print &#39;成功连接到 %s&#39; % (self.hostaddr) print &#39;开始登录到 %s&#39; % (self.hostaddr) ftp.login(self.username, self.password) print &#39;成功登录到 %s&#39; % (self.hostaddr) debug_print(ftp.getwelcome()) except Exception: deal_error("连接或登录失败") try: print "now:",self.ftp.pwd() self.ftp.cwd(self.remotedir) except(Exception): deal_error(&#39;切换目录失败&#39;) def is_same_size(self, localfile, remotefile): try: remotefile_size = self.ftp.size(remotefile) except: remotefile_size = -1 try: localfile_size = os.path.getsize(localfile) except: localfile_size = -1 debug_print(&#39;lo:%d re:%d&#39; % (localfile_size, remotefile_size), ) if remotefile_size == localfile_size: return 1 else: return 0 def download_file(self, localfile, remotefile): if self.is_same_size(localfile, remotefile): debug_print(&#39;%s 文件大小相同,无需下载&#39; % localfile) return else: print "remotefile:",remotefile debug_print(&#39;>>>>>>>>>>>>下载文件 %s ... ...&#39; % localfile) # return file_handler = open(localfile, &#39;wb&#39;) self.ftp.retrbinary(&#39;RETR %s&#39; % (remotefile), file_handler.write) file_handler.close() def download_files(self, localdir=&#39;./&#39;, remotedir=&#39;./&#39;): try: print "remotedir:",remotedir self.ftp.cwd(remotedir) except: debug_print(&#39;目录%s不存在,继续...&#39; % remotedir) return if not os.path.isdir(localdir): # pass os.makedirs(localdir) debug_print(&#39;切换至目录 %s&#39; % self.ftp.pwd()) self.file_list = [] print(self.ftp.dir()) self.ftp.dir(self.get_file_list) remotenames = self.file_list # print(remotenames) # return for item in remotenames: filetype = item[0] filename = item[1] print "filename:",filename local = os.path.join(localdir, filename).replace(&#39;\\&#39;, &#39;/&#39;) if filetype == &#39;d&#39;: self.download_files(local, filename) elif filetype == &#39;-&#39;: self.download_file(local, filename) self.ftp.cwd(&#39;..&#39;) debug_print(&#39;返回上层目录 %s&#39; % self.ftp.pwd()) def upload_file(self, localfile, remotefile): if not os.path.isfile(localfile): return if self.is_same_size(localfile, remotefile): debug_print(&#39;跳过[相等]: %s&#39; % localfile) return file_handler = open(localfile, &#39;rb&#39;) self.ftp.storbinary(&#39;STOR %s&#39; % remotefile, file_handler) file_handler.close() debug_print(&#39;已传送: %s&#39; % localfile) def upload_files(self, localdir=&#39;./&#39;, remotedir=&#39;./&#39;): if not os.path.isdir(localdir): return localnames = os.listdir(localdir) self.ftp.cwd(remotedir) for item in localnames: src = os.path.join(localdir, item) if os.path.isdir(src): try: self.ftp.mkd(item) except: debug_print(&#39;目录已存在 %s&#39; % item) self.upload_files(src, item) else: self.upload_file(src, item) self.ftp.cwd(&#39;..&#39;) def get_file_list(self, line): print "line1:", line ret_arr = [] file_arr = self.get_filename(line) print "file_arr:",file_arr if file_arr[1] not in [&#39;.&#39;, &#39;..&#39;]: self.file_list.append(file_arr) def get_filename(self, line): print "line2:",line print type(line) pos = line.rfind(&#39;:&#39;) while (line[pos] != &#39; &#39;): pos += 1 while (line[pos] == &#39; &#39;): pos += 1 print pos file_arr = [line[0], line[pos:]] return file_arr def debug_print(s): print (s) def deal_error(e): timenow = time.localtime() datenow = time.strftime(&#39;%Y-%m-%d&#39;, timenow) logstr = &#39;%s 发生错误: %s&#39; % (datenow, e) debug_print(logstr) file.write(logstr) sys.exit() if name == &#39;main&#39;: file = open("log.txt", "a") timenow = time.localtime() datenow = time.strftime(&#39;%Y-%m-%d&#39;, timenow) logstr = datenow # 配置如下变量 hostaddr = &#39;192.168.1.100&#39; # ftp地址 username = &#39;ftpuser3&#39; # 用户名 password = &#39;test1passwd&#39; # 密码 port = 21 # 端口号 #rootdir_local = &#39;.&#39; + os.sep + &#39;bak/&#39; # 本地目录 rootdir_local = &#39;D:/ftp/&#39; rootdir_remote = &#39;./&#39; # 远程目录 f = MYFTP(hostaddr, username, password, rootdir_remote, port) f.login() f.download_files(rootdir_local, rootdir_remote) timenow = time.localtime() datenow = time.strftime(&#39;%Y-%m-%d&#39;, timenow) logstr += " - %s 成功执行了备份\n" % datenow debug_print(logstr) file.write(logstr) file.close()
以上就是使用python ftp和sftp的实例介绍的详细内容,更多请关注 第一PHP社区 其它相关文章!