{"id":512,"date":"2011-01-17T19:19:30","date_gmt":"2011-01-17T19:19:30","guid":{"rendered":"https:\/\/fir3netwp.gmsrrpobkbd.com\/2011\/01\/17\/python-ftp-file-downloader\/"},"modified":"2021-08-01T09:18:09","modified_gmt":"2021-08-01T09:18:09","slug":"python-ftp-file-downloader","status":"publish","type":"post","link":"https:\/\/www.fir3net.com\/Programming\/Python\/python-ftp-file-downloader.html","title":{"rendered":"Python – Basic FTP Downloader"},"content":{"rendered":"
This python script will connect to a designated FTP server. Obtain a list of all files, then sort all files with a .jpa extension and then download the latest one.<\/p>\n
In order to do this regular expressions are used to build a list of files only containing .jpa file extensions. This list is then sorted by each of the integers contained within each file name.<\/p>\n
#!\/usr\/bin\/python\r\n#\r\n# Usage : Download latest .jpa file from FTP Server.\r\n\r\nimport ftplib ## IMPORT MODULES\r\nimport string ## IMPORT MODULES\r\nimport os ## IMPORT MODULES\r\nimport re ## IMPORT MODULES\r\n\r\nftpserver = \"ftp.server\" ## ASSIGN VARIABLE\r\nusername = \"username\" ## ASSIGN VARIABLE\r\npassword = \"password\" ## ASSIGN VARIABLE\r\nlocaldir = \"\/path\/example\" ## ASSIGN VARIABLE\r\n\r\ndata = [] ## SET EMPTY LIST\r\nlist2 = [] ## SET EMPTY LIST\r\n\r\nos.chdir(localdir) ## CHANGE LOCAL DIRECTORY\r\n\r\nftp = ftplib.FTP(ftpserver) ## SET FTPLIB MODULE TO FTPSERVER + ASSIGN TO FTP\r\n\r\ntry:\r\n ftp.login(username, password) ## LOG INTO FTP SERVER\r\n\r\n ftp.dir(data.append) ## GET DIRECTORY LISTING + ASSIGN TO LIST\r\n\r\n ## CONVERT DIR LISTING LINES TO WORDS FOR EACH ELEMENT\r\n data = \" \".join(data) ## CONVERT DATA LIST INTO STR\r\n data = data.split() ## CONVERT DATA STR INTO LIST\r\n\r\n y = re.compile('.*\\.jpa$') ## SET REGEX MATCH CRITERIA\r\n\r\n for x in data: ## LOOP THROUGH DATA LIST + APPEND ...\r\n if y.match(x): ## ... ANY ELEMENT MATCHING REGEX TO LIST2.\r\n list2.append(x)\r\n\r\n dstfile = (' '.join(sorted(list2, key = lambda x:x[-19:-4]))) ## SORT LIST2 BY NUMERIC DIGITS WITHIN ELEMENTS + ASSIGN TO STR\r\n dstfile = dstfile.split() ## SPLIT STR TO LIST\r\n dstfile = dstfile[-1] ## ASSIGN LAST ELEMENT OF LIST TO STR\r\n\r\n ftp.retrbinary('RETR '+dstfile, open(dstfile, 'wb').write) ## GET FILE\r\n\r\n ftp.close() ## CLOSE FTP CONNECTION GRACEFULLY\r\n\r\nexcept Exception, e:\r\n print e\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"This python script will connect to a designated FTP server. Obtain a list of all files, then sort all files with a .jpa extension and then download the latest one. In order to do this regular expressions are used to build a list of files only containing .jpa file extensions. This list is then sorted … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"yoast_head":"\n
Python - Basic FTP Downloader - Fir3net<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n