1.

Solve : ftp commands?

Answer» <html><body><p>Hi all,<br/><br/>I'm cracking my head with a small and simple thing, that unfortunately seems out of reach for my head...<br/>I need to transfer files from a ftp server to my computer in a automatic way. The problem is that the <a href="https://interviewquestions.tuteehub.com/tag/folder-246959" style="font-weight:bold;" target="_blank" title="Click to know more about FOLDER">FOLDER</a> in the ftp, were these files are, acts as a backup as well, having files with 3 weeks <a href="https://interviewquestions.tuteehub.com/tag/old-585313" style="font-weight:bold;" target="_blank" title="Click to know more about OLD">OLD</a>. Is there a way of <a href="https://interviewquestions.tuteehub.com/tag/sorting-11834" style="font-weight:bold;" target="_blank" title="Click to know more about SORTING">SORTING</a> the files by date, and just download these ones instead of everything? <br/><br/>Thanks in advance<br/><br/>Regardsthe windows ftp client that came with <a href="https://interviewquestions.tuteehub.com/tag/installation-16361" style="font-weight:bold;" target="_blank" title="Click to know more about INSTALLATION">INSTALLATION</a> is primitive. Instead, use a programming language, eg Python (or any others) that provides you FTP libraries for file transfer. eg in Python<br/> Code: <a>[Select]</a>import ftplib<br/>server="localhost"<br/>user="anonymous"<br/>password="<a href="/cdn-cgi/l/email-protection">[email protected]</a>"<br/>filelist=[]<br/><a href="https://interviewquestions.tuteehub.com/tag/def-431442" style="font-weight:bold;" target="_blank" title="Click to know more about DEF">DEF</a> download(ftp, filename):<br/>    ftp.retrbinary("RETR " + filename, open(filename,"wb").write)<br/>try:<br/>   ftp = ftplib.FTP()<br/>   ftp.debug(3)<br/>   ftp.connect(server,21)<br/>   ftp.login(user,password)<br/>except Exception,e:<br/>    print e<br/>else:<br/>    ftp.cwd("directory to change if any")<br/>    ftp.retrlines('LIST',filelist.append)<br/>    for latest in filelist:<br/>        if latest.startswith("-") and "latest file pattern if any" in latest:<br/>            print latest<br/>            download(ftp,latest)<br/>    ftp.quit()<br/>sys.exit()<br/><br/>this assumes that your old files are named in a distinct format, such as file.bak or something, and your latest file is not in that format.<br/><br/>usage:<br/> Code: <a>[Select]</a>C:\test&gt; python myscript.py<br/></p></body></html>


Discussion

No Comment Found