Python Script List Files on FTP server


Python List Files on FTP server

Below script is a Python Script to List Files on FTP server.Script requires user input to locate properties file. Script than reads properties file for hostname, username and password information using ConfigParser and then using ftplib to create connection to the ftp server to list all files and directories. ftp.properties


[FTP]
hostname = ftp.mycomp.com
username = test
password = test072
#! /usr/bin/python
################################################# 
# Name : Create User WLST Script 
# Author : Togotutor 
# Copyright - 2014 www.togotutor.com 
################################################ 
import ConfigParser
import getopt
import sys
from ftplib import FTP
class ftpmodule:
    def __init__(self):
        self.propertiesfile = ''
    def usage(self):
        print('Please pass arguments to this program - {format} - ftp_check.py -i ftp.properties')
    def _getprops(self):
        if len(sys.argv) == 1 :
            ftpmodule.usage(ftpmodule())
            sys.exit()
        else:
            try:
                opts, args = getopt.getopt(sys.argv[1:],"hi:",["props="])
            except getopt.GetoptError:
                print ('ftp_check.py -i ftp.properties')
                sys.exit(2)
            for opt, arg in opts:
                if opt == '-h':
                    print ('ftp_check.py -i ftp.properties')
                    sys.exit()
                elif opt in ("-i""--props"):
                    self.propertiesfile = arg
        return self.propertiesfile
    def _connecttoFTP(self):
        config = ConfigParser.ConfigParser()
        config.read(self.propertiesfile)
        hostname = config.get('FTP''hostname')
        username = config.get('FTP''username')
        password = config.get('FTP''password')
        ftp = FTP(hostname, username, password)
        ftp.dir()

def main():
    ftpmod = ftpmodule()
    ftpmod._getprops()
    ftpmod._connecttoFTP()
    

if __name__ == '__main__':main()