Coding in Phyton


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Coding in Phyton
# 1  
Old 11-26-2010
Coding in Phyton

I have the following Phyton script and want to print something if the option was selected

For example if the user uses --npop=12, I want to check if the user has inputted --npop and
do

if npop selected, print npop

Or if the user puts --fdata=npt02.dat I print something like

if fdata selected, print fdata




Code:
#! /usr/bin/python

import getopt
import popen2
import sys
import types

__author__="C Dimech"
__date__ ="$06-Nov-2010 00:18:43$"

if __name__ == "__main__":

    tpath = None;
    fbase = None
    fdata = None;
    fstmod = None;
    frestore = None;
    ftag = None;
    tee = None;
    nxz = None;
    nxp = None;
    nzp = None;
    npop = None;
    varp = None;
    varpint = None;
    varpfrc = None;
    sigma = None;
    maxiter = None;
    tau = None;
    mdacc = None;
    mindist = None;
    axitertp = None;
    tpf = None;
    vrb = None;
    Version="V03"

    long_opts= [
    "tpath=",
    "fbase=",
    "fdata=",
    "fstmod=",
    "frestore=",
    "ftag=",
    "tee=",
    "nxz=",
    "npop=",
    "varp=",
    "sigma=",
    "maxiter=",
    "dtau=",
    "mdacc=",
    "mindist=",
    "maxitertp=",
    "tpf=",
    "vrb="]

    try:
        
        optlist, args = getopt.getopt(sys.argv[1:], "", long_opts)

    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        print_usage()
        sys.exit(2)

    for opt, val in optlist:
        if opt == "--tpath":
            tpath = val
        elif opt == "--fbase":
            fbase = val.split('.base')[0];
        elif opt == "--fdata":
            fdata = val.split('.dat')[0];
        elif opt == "--fstmod":
            fstmod = val.split('.cmod')[0];
        elif opt == "--frestore":
            frestore = val
        elif opt == "--nxz":
            nxp = val.split('x')[0];
            nzp = val.split('x')[1];
        elif opt == "--npop":
            npop = val
        else:
            print ""
            print "ERROR: Unrecognised argument,",opt
            print ""
            sys.exit(-1)


Last edited by kristinu; 11-27-2010 at 07:31 PM..
# 2  
Old 11-27-2010
Line 77, Try changing

Code:
'''%(Version)

to this:

Code:
%(Version)'''

# 3  
Old 11-27-2010
Does not work

---------- Post updated at 06:33 PM ---------- Previous update was at 01:12 PM ----------

I would like to output some print statements when the user selects an option using a command line argument.

---------- Post updated at 07:15 PM ---------- Previous update was at 06:33 PM ----------

I have tried the code below but am getting the error

Code:
  File "tdarwin.py", line 395
    print " -tpath = %s\n",%(tpath)
                           ^
SyntaxError: invalid syntax


Code:
    if (not isinstance(tpath,types.NoneType)):
        print " -tpath = %s\n",%(tpath)

# 4  
Old 11-28-2010
Here is how you do it:

One argument example:
Code:
>>> p1 = "path1"
>>> p2 = "path2"
>>> print "result: %s" % (p1)
result: path1

Two argument example:
Code:
>>> print "results: %s, %s" % (p1,p2)
results: path1, path2
>>>

Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Need help with coding

HI, Can some one guide me how to make changes to the script below so that it can load the history of a program to IT server ? Format of data: YYYYMMDD065959.dsk.log YYYYMMDD235959.dsk.log currently both are loaded together. Need to separate them as above format. Thanks in advance. ... (2 Replies)
Discussion started by: crazydude80
2 Replies

2. Shell Programming and Scripting

Need help with coding

HI, Can some one guide me how to make changes to the script below so that it can load the history of a program to IT server ? Format of data: YYYYMMDD065959.dsk.log YYYYMMDD235959.dsk.log currently both are loaded together. Need to separate them as above format. Thanks in advance. ... (1 Reply)
Discussion started by: crazydude80
1 Replies

3. Red Hat

installing phyton in centos

Hi, I am trying to install phyton on my centos. When I write: yum install phyton I get the message that it doesn't find the package. How should I fix it? (4 Replies)
Discussion started by: programAngel
4 Replies

4. AIX

need help for coding this logic

contact me on <email address deleted> or <email address deleted> (1 Reply)
Discussion started by: suprithhr
1 Replies

5. UNIX for Advanced & Expert Users

can I use this coding

I apologise because I had pasted this question in the newbies forum first (because i am a bit of a newbie) but thought it might be better suited in here if i have to sepearate parameters can I use this syntax especially the or part (||) and is this correct if (6 Replies)
Discussion started by: w33man
6 Replies
Login or Register to Ask a Question