2016-10-03 29 views
-2

使用python 我用這個代碼,但它不工作了如何從一個文件夾使用python

ftp=FTP("host") 
ftp.login("user name","password") 
def downloadFiles(path,destination): 
    try: 
     ftp.cwd(path) 
     #clone path to destination 
     ftp.dir(destination) 
     #~ os.mkdir(destination[0:len(destination)-1]+path) 
     print destination[0:len(destination)-1]+path+" built" 
    except OSError: 
     #folder already exists at destination 
     pass 
    except ftplib.error_perm: 
     #invalid entry (ensure input form: "/dir/folder/something/") 
     print "error: could not change to "+path 
     sys.exit("ending session") 

    filelist=ftp.nlst() 

    for file in filelist: 
     try: 
      #this will check if file is folder: 
      ftp.cwd(path+file+"/") 
      #if so, explore it: 
      downloadFiles(path+file+"/",destination) 

     except ftplib.error_perm: 
      #not a folder with accessible content 
      #download & return 
      #~ os.chdir(destination[0:len(destination)]+path) 
      #possibly need a permission exception catch: 
      #~ ftp.retrbinary("RETR "+ file, open(ftp.path.join(destination,file),"wb").write) 
      ftp.storlines("STOR "+file, open(ftp.dir(destination, file),'r')) 
      print file + " downloaded" 
    return 
+0

歡迎來到Stackoverflow!準確地說:a)到目前爲止你所嘗試的是什麼(展示一個**最小完整的可驗證示例**)b)你卡在哪裏(再次顯示代碼)。 – SuperSaiyan

+0

嘗試'ftplib'模塊和'rename()'函數。 – furas

+0

@steven Barnes請參閱下面的問題我使用該代碼 –

回答

0

在同一FTP從一個文件夾如何將文件移動到另一個文件夾移動文件到另一個文件夾相同的ftp我會建議使用優秀的Python文件系統抽象pyfs,你可以從documents看到所有的文件系統,一旦被安裝,有copycopydirmovemovedir方法,個人而言,我總是會複製,驗證然後刪除安全特別在遠程系統上。

+0

我下載了本地系統中的ftp文件,它的工作正常,但是它不在同一個ftp文件夾中工作 –

+0

第一個問題當然是你的帳戶對服務器和目錄都有__write__訪問權限。 –

相關問題