用户登录
用户注册

分享至

如何使用 Python ftplib 通过 FTP 下载文件

  • 作者: 花江猫狗肉馆馆主
  • 来源: 51数据库
  • 2022-10-21

问题描述

I have the following code which easily connects to the FTP server and opens a zip file. I want to download that file into the local system. How to do that?

# Open the file for writing in binary mode
print 'Opening local file ' + filename
file = open(filename, 'wb')

# Download the file a chunk at a time
# Each chunk is sent to handleDownload
# We append the chunk to the file and then print a '.' for progress
# RETR is an FTP command

print 'Getting ' + filename
ftp.retrbinary('RETR ' + filename, handleDownload)

# Clean up time
print 'Closing file ' + filename
file.close()

解决方案

handle = open(path.rstrip("/") + "/" + filename.lstrip("/"), 'wb')
ftp.retrbinary('RETR %s' % filename, handle.write)

软件
前端设计
程序设计
Java相关