Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list of ftp sites ( eg:5 ) in text file and i need to download the last created file from ftp sites. Is this possible. This is my code :

Python
import os
from ftplib import FTP

ftp = FTP("xxx.xx.xx.xx1", "USERNAME1", "PASSWORD1")
ftp = FTP("xxx.xx.xx.xx2", "USERNAME2", "PASSWORD2")
ftp = FTP("xxx.xx.xx.xx3", "USERNAME3", "PASSWORD3")
ftp = FTP("xxx.xx.xx.xx4", "USERNAME4", "PASSWORD4")
ftp = FTP("xxx.xx.xx.xx5", "USERNAME5", "PASSWORD5")

ftp.login()
ftp.retrlines("LIST")

ftp.cwd("SmythIN/2014-10-29") --- here  i have a folder created by current  date ...how can i pass current date folder i change directory.
ftp.cwd("subFolder") # or ftp.cwd("folderOne/subFolder")

listing = []
ftp.retrlines("LIST", listing.append)
words = listing[0].split(None, 8)
filename = words[-1].lstrip()

# download the file
local_filename = os.path.join(r"c:\myfolder", filename)
lf = open(local_filename, "wb")
ftp.retrbinary("RETR " + filename, lf.write, 8*1024)
lf.close() 
Posted
Comments
Richard MacCutchan 12-Mar-15 7:31am    
What happens when you try it?

1 solution

Python
import time

accounts = (
    ("xxx.xx.xx.xx1", "USERNAME1", "PASSWORD1"),
    ("xxx.xx.xx.xx2", "USERNAME2", "PASSWORD2"),
    ("xxx.xx.xx.xx3", "USERNAME3", "PASSWORD3"),
    ("xxx.xx.xx.xx4", "USERNAME4", "PASSWORD4"),
    ("xxx.xx.xx.xx5", "USERNAME5", "PASSWORD5"),
)


TODAY = time.strftime("%Y-%m-%d")################################

for account in accounts:
    ftp = FTP(*account)####################################
    ftp.login()
    ftp.retrlines("LIST")#<-this statement looks useless.

    ftp.cwd("SmythIN/"+TODAY)################################----- it breaks in here.
    ftp.cwd("subFolder") # or ftp.cwd("folderOne/subFolder")

    listing = []
    ftp.retrlines("LIST", listing.append)
    words = listing[0].split(None, 8)
    filename = words[-1].lstrip()

    # download the file
    local_filename = os.path.join(r"c:\myfolder", filename)
    with open(local_filename, "wb") as lf:
        ftp.retrbinary("RETR " + filename, lf.write, 8*1024)




ftp.cwd("SmythIN/"+TODAY)################################

its not SmythIN on all ftps it has different names like
ftp.cwd("X/"+TODAY)################################
ftp.cwd("Y/"+TODAY)################################
ftp.cwd("Z/"+TODAY)################################

how to write this...... thanks in advance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900