본문 바로가기
파이썬

파이썬 Python ftplib로 FTP를 통해 파일을 다운로드하는 방법

by º기록 2021. 2. 12.
반응형

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()

 

해결 방법

 

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

 

참조 페이지 https://stackoverflow.com/questions/11573817

 

 

반응형

댓글