2013-08-17 3 views
0

아래 스크립트는 다운로드하려는 실제 파일이 아닌 FTP 사이트에서 파일 이름 만 다운로드하므로 파일 크기가 0 인 이유는 무엇입니까? 문제의 원인은 무엇입니까? 어떤 도움이라도 대단히 감사하겠습니다.Python - ftp 디렉토리에서 모든 파일 다운로드

import os 
from ftplib import FTP 
import time 


# Connects to remote ftp server 
def connect_ftp(hostname, username, password, source_directory, destination_directory): 
    os.chdir(destination_directory) 

    print("Connecting to " + hostname + "...") 
    ftp = FTP(hostname) 
    ftp.login(username, password) 

    print(ftp.getwelcome()) 
    print("Connected Successfully!\n") 

    ftp.cwd(source_directory) 

    download_files(ftp) 


# Download files 
def download_files(ftp): 
    print("This script will only download files, not directories.") 
    print("Files at " + source_directory + ":\n") 
    ftp.retrlines("LIST") 

    file_list = [] 
    ftp.retrlines("NLST", file_list.append) 

    print("") 

    i = 0 
    for filename in file_list: 
     if (filename != '.') and (filename != '..'): 
      print("Downloading " + filename + "...") 
      try: 
       ftp.retrbinary("RETR " + filename, open(filename, "wb").write) 
       i=i+1 
      except Exception as directory_error: 
       print ("Oops! Was " + filename + " a directory? I won't download directories.") 

    print(str(i) + " files successfully downloaded.\n") 

    disconnect_ftp(ftp) 


# Disconnects from ftp server 
def disconnect_ftp(ftp): 
    print("Disconnecting from " + hostname + "...")  
    ftp.quit() 
    print("Disconnected from " + hostname + ".") 
    time.sleep(4) 


hostname = "ftpsite"         # TODO: Replace with the hostname of the server you want to connect to 
username = ""              # TODO: Replace the username 
password = ""              # TODO: Replace the password 
source_directory = "/source/directory/" # TODO: Change location to wherever you want to start the download 
destination_directory = "C:\example\FTP"       # TODO: Change the location of where you want to download the files to 

connect_ftp(hostname, username, password, source_directory, destination_directory) 
+0

디렉토리의 모든 파일을 다운로드 디렉토리를 다운로드로 본질적으로 동일, 처음에 한 것과 같은 이름의 대상 디렉토리를 생성하기 만하면 모든 파일의 출처 인 소스. – martineau

+0

@martineau 귀하의 제안에 감사드립니다. – user

+0

파일을 다운로드하지만 파일 크기는 0KB입니다. "0 파일을 성공적으로 다운로드했습니다". – user

답변

-1

왜 파이썬을 사용합니까? * nix에서 스크립트 시스템 쉘에서 실행 명령을

wget -r ftp://source/directory 
+0

파이썬을 사용하도록 요청 했으므로 다음을 작성해야합니다. 이것에 대한 GUI. 뿐만 아니라 다운로드 프로세스. – user

관련 문제