Sample Upload and Download File to FTP Using Python

Pada posting berjudul "Sample Upload and Download File to FTP Using Python" ini. Saya berikan sample script python untuk melakukan download file dari FTP ke local folder dan upload file dari local folder ke FTP.

Versi Python yang saya gunakan adalah  3.6.0 dan sample skrip dibawah ini bisa di-download pada link berikut ini : Upload Donwload FTP using Python

Yang perlu diperhatikan dalam program download dan upload adalah source ftp dan local folder berada pada lokasi yang berbeda. Untuk path ftp kita akan menggunakan ftp.cwd untuk mentukan lokasi file di ftp sedangkan untuk local folder maka kita gunakan os.chdir.

import os, time
from ftplib import FTP
ftp_obj = FTP()

server = "172.16.0.5"
port = 21
usr = ""
pwd = ""

ftp_obj.connect(server, port)
ftp_obj.login(usr, pwd)

ftpfolder = "/Transfer/IT/Agus"
locfolder = "E:\PROJECT\PYTHON\File"
interval = 0.05

def downloadFiles(ftppath, locpath):
    ftp_obj.cwd(ftppath)
    filelist = ftp_obj.nlst()
    os.chdir(locpath)
    for file in filelist:
        time.sleep(interval)
        print("downloading file : " + file)
        localfile = open(file, 'wb')
        ftp_obj.retrbinary('RETR ' + file, localfile.write, 1024)
        localfile.close()

def uploadFiles(ftppath, locpath):
    ftp_obj.cwd(ftppath)
    filelist = os.listdir(locpath)   
    os.chdir(locpath)
    for file in filelist:
        time.sleep(interval)
        print("uploading file : " + file)
        ftp_obj.storbinary('STOR ' + file, open(file, 'rb'))
       
downloadFiles(ftpfolder, locfolder)
# uploadFiles(ftpfolder, locfolder)

ftp_obj.quit()



Semoga posting tentang "Sample Upload and Download File to FTP Using Python" diatas dapat bermanfaat.


Salam

Popular posts from this blog

SmartObject property ID is a required property for selected method Create. Value must be set.

Cara inject USB 3.0 Driver pada instalasi Windows - How to Inject USB 3.0 Driver in Windows 7

Python Font Color in Console