python,如何下载FTP上的文件
2个回答
展开全部
import ftplib, socket
def connect():
CONST_HOST = "xxxx.xxxx.xxx"
CONST_USERNAME = "xxxxx"
CONST_PWD = "xxxxxxx"
try:
ftp = ftplib.FTP(CONST_HOST)
ftp.login(CONST_USERNAME, CONST_PWD)
return ftp
except socket.error, socket.gaierror:
print("FTP is unavailable,please check the host,username and password!")
sys.exit(0)
def disconnect(ftp):
ftp.quit()
def download(ftp, filename):
#预定义每次写文件的buffer
CONST_BUFFER_SIZE = 8192
f = open(filename, "wb").write
try:
ftp.retrbinary("RETR %s" % filename, f, CONST_BUFFER_SIZE)
except ftplib.error_perm:
return False
return True
def find(ftp, filename):
ftp_f_list = ftp.nlst()
if filename in ftp_f_list:
return True
else:
return False
ftp = connect()
#yourfile为你想要下载的文件
if find(ftp, 'yourfile'):
download(ftp, 'yourfile')
disconnect(ftp)
展开全部
通过python下载FTP上的文件夹的实现代码:
# -*- encoding: utf8 -*-
import os
import sys
import ftplib
class FTPSync(object):
def __init__(self):
self.conn = ftplib.FTP('10.22.33.46', 'user', 'pass')
self.conn.cwd('/') # 远端FTP目录
os.chdir('/data/') # 本地下载目录
def get_dirs_files(self):
u''' 得到当前目录和文件, 放入dir_res列表 '''
dir_res = []
self.conn.dir('.', dir_res.append)
files = [f.split(None, 8)[-1] for f in dir_res if f.startswith('-')]
dirs = [f.split(None, 8)[-1] for f in dir_res if f.startswith('d')]
return (files, dirs)
def walk(self, next_dir):
print 'Walking to', next_dir
self.conn.cwd(next_dir)
try:
os.mkdir(next_dir)
except OSError:
pass
os.chdir(next_dir)
ftp_curr_dir = self.conn.pwd()
local_curr_dir = os.getcwd()
files, dirs = self.get_dirs_files()
print "FILES: ", files
print "DIRS: ", dirs
for f in files:
print next_dir, ':', f
outf = open(f, 'wb')
try:
self.conn.retrbinary('RETR %s' % f, outf.write)
finally:
outf.close()
for d in dirs:
os.chdir(local_curr_dir)
self.conn.cwd(ftp_curr_dir)
self.walk(d)
def run(self):
self.walk('.')
def main():
f = FTPSync()
f.run()
if __name__ == '__main__':
main()
# -*- encoding: utf8 -*-
import os
import sys
import ftplib
class FTPSync(object):
def __init__(self):
self.conn = ftplib.FTP('10.22.33.46', 'user', 'pass')
self.conn.cwd('/') # 远端FTP目录
os.chdir('/data/') # 本地下载目录
def get_dirs_files(self):
u''' 得到当前目录和文件, 放入dir_res列表 '''
dir_res = []
self.conn.dir('.', dir_res.append)
files = [f.split(None, 8)[-1] for f in dir_res if f.startswith('-')]
dirs = [f.split(None, 8)[-1] for f in dir_res if f.startswith('d')]
return (files, dirs)
def walk(self, next_dir):
print 'Walking to', next_dir
self.conn.cwd(next_dir)
try:
os.mkdir(next_dir)
except OSError:
pass
os.chdir(next_dir)
ftp_curr_dir = self.conn.pwd()
local_curr_dir = os.getcwd()
files, dirs = self.get_dirs_files()
print "FILES: ", files
print "DIRS: ", dirs
for f in files:
print next_dir, ':', f
outf = open(f, 'wb')
try:
self.conn.retrbinary('RETR %s' % f, outf.write)
finally:
outf.close()
for d in dirs:
os.chdir(local_curr_dir)
self.conn.cwd(ftp_curr_dir)
self.walk(d)
def run(self):
self.walk('.')
def main():
f = FTPSync()
f.run()
if __name__ == '__main__':
main()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询