News wrote: > Hi everyone, > > My goal is to pull command switches/options from a file and then assign > the values to select variables which would eventually be included in a > class object. > > The data file looks something like this but the switches could be in any > order and not all may be used. > > -m quemanager -s server -p port -k key -o object -c 20 -t [EMAIL PROTECTED] > > Also, please keep in mind that the source code will have more than one > line in it and each has to be treaded separately. > > > In a first pass, I wrote the following code which works but there is > probably a better way of doing it. > > Any ideas to make it more efficient/stream-lined would be greatly > appreciated. > > #!/usr/bin/python > > import string > inp = open("const.txt","r") > # > # Read File > # > > while True: > > # > # Get line from file > # > line=inp.readline() > > # > # Check for EOF or break line up to extract relevant pieces > # > if len(line) == 0: > break > else: > split_line=line.split() > length=len(split_line) > count=0 > > # > # Evaluate list item and assign variable based on its contents > # Print statements are for debugging purposes only > # > for i in range(length): > if split_line[count] == "-m": > qmgr=split_line[count+1] > print "Queue Manager",qmgr; > elif split_line[count] == "-s": > server=split_line[count+1] > print "Server",server; > elif split_line[count] == "-p": > port=split_line[count+1] > print "Port",port; > elif split_line[count] == "-o": > object=split_line[count+1] > print "Object",object; > elif split_line[count] == "-k": > key=split_line[count+1] > print "Key",key; > elif split_line[count] == "-t": > mto=split_line[count+1] > print "To",mto; > elif split_line[count] == "-c": > check=split_line[count+1] > print "Check",check; > elif split_line[count] == "-d": > report="" > print "Report",report; > elif split_line[count] == "-q": > display=False > print "Display",display; > else: > continue > > count=count+1 > > # Close input file > # > inp.close() >
Others have addressed your specific question. I'm going to make a different suggestion. Change your thinking so that you can use ConfigParser to get your values from a .INI/.CONF file instead. The file would look something like: [section001] m=queuemanager server=myserver port=1080 key=somekey object=someobject c=20 [EMAIL PROTECTED] [section002] . . If you have many of these just put things in a loop . and process each section . You can then read with (not tested): import ConfigParser inifilename='program.ini' INI=ConfigParser.ConfigParser() INI.read(inifilename) section='section001' option='m' try: m=INI.get(section, option) except: m=None option='server' try: server=INI.get(section, option) except: server=None option='port' try: port=INI.getint(section, option) except: port=None # Or set to default port option='key' try: key=INI.get(section, option) except: key=None option='object' try: object=INI.get(section, option) except: object=None option='c' try: c=INI.getint(section, option) except: c=None option='emailtolist' try: emailtolist=INI.get(section, option).split(';') except: emailtolist=None Just a suggestion. -Larry Bates -- http://mail.python.org/mailman/listinfo/python-list