2016-07-05 3 views
0

다운로드가 파이썬에서 작동하지 않습니까?python 스크립트를 사용하여 FTP 위치에서 디렉토리/파일을 다운로드하십시오.

FTP 위치에서 파일을 가져 오기 위해 간단한 파이썬 프로그램을 작성했지만 실행하면 [Errno 13] Permission denied 메시지가 표시됩니다.

내 코드는 다음과 같습니다. 왜 그것이 작동하지 않는지 아는가?

import ftplib 
from ftplib import FTP, error_perm 

def getFTPDir(dirpath): 

    f = ftplib.FTP(ip, username, password) 

    try: 
     f.cwd(dirpath) 
     nameList = f.nlst() 
     oldest = nameList[0] 
     newest = nameList[-1] 

     newest = oldest 

     newDirPath = dirpath +'/'+ newest 

     print f.cwd(newDirPath) 
     subNameList = f.nlst() 

     for i in range (len(subNameList)): 
      f.cwd(newDirPath + '/' + str(subNameList[i])) 
      nameList1 = f.nlst() 

      filename = nameList1[i] 
      print "downloading..............", filename 


      f.retrbinary('RETR '+ filename, open(os.path.join(destination,localPath),"wb").write) 
      print filename + " downloaded" 

      try: 
       fhandle = open(filename, 'wb') 
       f.retrbinary('RETR ' + filename, fhandle.write) 

      except Exception, e: 
       print str(e) 

      finally: 
       fhandle.close() 

    except error_perm: 
     return 

    except Exception, e: 
     print str(e) 

    finally: 
     f.close() 
+0

오류가 발생 줄 무엇을 말 해주세요 ... 당신이 작성하려고 곳

try: localname = os.path.join(destination,localPath) # a spy to control the names, comment it out when it works print filename, " -> ", localname with open(localname, 'wb') as fhandle f.retrbinary('RETR ' + filename, fhandle.write) except Exception as e: print str(e) 

그런 식으로, 당신이 볼 수 : UT는 다음 줄에 쓴 일을 계속, 또는 더 나은 함께 사용 ... –

+0

f.retrbinary ('RETR'+ 파일 이름, 열려있는 (os.path.join (대상, 로컬 경로), "wb" – Dush

답변

0

FTPLIB의 문서는 (광산을 강조) 말한다 :

이진 전송 모드에서 파일을 검색

FTP.retrbinary (명령, 콜백 [maxblocksize는 [,] 휴식]) . 명령은 적절한 RETR 명령이어야합니다 : 'RETR filename'. 콜백 함수는 수신 된 데이터의 각 블록에 대해 호출되며 단일 문자열 인수는 데이터 블록을 제공합니다.

  • destinationlocalPath 비 존재 폴더 또는 BTW 이외의 쓰기 가능한 폴더 또는 파일 (가리 수, 각 반복에 같은 파일을 다시 것 :

그래서이 가능한 오류가 여기에 원인이있다 ...)

  • 하나 개의 블록에 옮겨진하지에 파일이, 당신은 아무것도
  • 그러지 마, B를 닫지 않고 각 블록의 대상 파일을 열려고하면

    관련 문제