У меня есть следующий код, который легко соединяется с FTP-сервером и открывает zip-файл. Я хочу загрузить тот файл в локальную систему. Как сделать это?
# 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()
Отметьте, загружаете ли Вы от FTP до Вашего локального, необходимо будет использовать следующее:
with open( filename, 'wb' ) as file :
ftp.retrbinary('RETR %s' % filename, file.write)
Иначе, сценарий будет в Вашем локальном хранилище файлов, а не FTP.
Проводят несколько часов, делая ошибку самостоятельно.
Сценарий ниже:
import ftplib
# Open the FTP connection
ftp = ftplib.FTP()
ftp.cwd('/where/files-are/located')
filenames = ftp.nlst()
for filename in filenames:
with open( filename, 'wb' ) as file :
ftp.retrbinary('RETR %s' % filename, file.write)
file.close()
ftp.quit()