Hey Guys :

Here The problem is :to have a program to check the

-(sys.argv) is long enough to have more than 3 elements if not then print "input more "
-check (sys.argv) contain a specific elements for ex(P) ,& if P is found in the given elements then it should also check for P+3(that mean the 4th elements after p) if also P then print "fount at )then the position at which the 1st P is found !!

the program can be run from the :Administrator: command Prompt ..

well I need a little guidence to solve this : here what i have done so far though I wont think this is enough at all !!

import sys
for i in (sys.argv):
    if "p  p" in (sys.argv):
        print yes

the below example ,I managed to solved it myself & (Not related to the above problem )to be run from command Prompt /Administrator :

the problem was to type what ever you type after executing the program in the command prompt & using (import sys & also sys.argv) :
for example try to point where is your file is :

c:\users\robert\desktop\name of your file.type anything

after executing the above it will appear like this :

3 arguments

& then it enumerate the name & length of each ...

import sys
print "No.of arguments is ",len(sys.argv)
print"  "
print "Now I will Print the Arguments & Length of each";print "  "
for i in (sys.argv):
    print i,", length ",len(i)

Recommended Answers

All 5 Replies

The first snippet checks whether "p p" is in sys.argv once for every item that is in sys.argv. It also won't run: The variable yes is not defined.

The second snippet looks ok. To do the "find the 3rd arg after the P arg" part, you need to know about method index:

if P in sys.argv:
  theIndexOfP = sys.argv.index(P)
  if len(sys.argv) <= theIndexOfP + 3:
    print >> sys.stderr, "OOPS, not enough args past P"
  else:
    print "OK, Third past P is",sys.argv(theIndexOfP+3)
else:
  print >> sys.stderr, "OOPS, no P"

you should look up the getopt module. I believe it will accomplish what you want.

Umm ..I am afraid it did not work neither ...

For getopt ,I Do not know it / and it is required to solve this Q with :import sys & regular expression (re.compile)

Here it is a summary :

suppose you have the sequence given in sys.argv command line :

motif=axxa

1-find if (axxa) is found in the command line argument ?(this mean it will check the 1st & 4th position & in between is not important )

2- if found then print (axxa) [here xx represent any 2 letters & it also should be printed with our motif .i.e instead of printing axxa then it should point to a+ the 2 letters found in the command argument line +a )

3-give the position where our motif (axxa) is found in the sys.argv command line argument .
4- if not found ------print not found
5-if not enough sequence or letters given in sys.argv.command line argument then print : please provide more info .!

I tried to use this : but it needs a bit of correction ..

import sys
import re
motif=("a,,a")
for i in (sys.argv):
    i=re.compile(motif)
    print "found"
    i.index(motif)
else :
    print "not found"

in the above when you execute it from the command line shell : it will print found as long as you have words in your command line argument /how can I make it short just to the axxa ?(check just the 1st & 4th position )
or also tried this one but didnt work ::??

import sys
import re

motif="a,,a"
for i in (sys.argv):
    pattern=re.compile(motif)
    print pattern
    i.index(motif)

Ok may be i should make it clearer :

the output should be like this & it is done by one program :

>C:\users\Robert\Desktop>your file name.py abserauwasjkida  
>>axxa(auwa)motif at positon 6

>C:\users\Robert\Desktop>your file name.py aswejdsheaj
>>Your sequence does not contain axxa motif 

C:\users\Robert\Desktop>your file name.py
>>please provide some line command line argument

Your regular expression is incorrect. "Any character" is . not , You cannot use aString.index(regularExpression) but must use match = regularExpression.search(aString) . Once you have a match object it is either None or can be examined for indices:

if match:
  print "The index of the match is %d"%match.start()
else
  print "There was no match"

When I see the code you are trying to write, I recognize the signs of panic or sleep deprivation: You are not thinking about what the program should do, you are just trying anything to see what happens. For instance, you make a regular expression object named pattern but you never use it. Please stop for ten minutes, go get a drink of water, look at the sky for two minutes, and think of something relaxing. Then come back to the problem and work on it like this:
make a file named myProblem0.py and put in a tiny 'hello world' code
work on that file until it is right, and it prints correctly
add a regular expression, but do not use it. Save it in a file named myProblem1.py
work on that file until it runs
now add code to use the regular expression, but not from sys.argv yet. Save it in a file named myProblem2.py
work on that file until it runs and does the right thing.
...
I hope you see the pattern of very small steps, and each step saved in its own file. Both parts are important: The small steps mean you always have something that works. This feels good and makes it easier to think (no panic). The saved files mean that if you make a huge mistake, you can just go back to the file that did work, and start again from there: Very little effort to get back to something that works. This feels good and prevents panic.

P.S. You could use a source control system instead of saving your code in numbered files, but I do not want to add anything for you to learn how to use. If you already know how to use source control, and have it already working, then please do use it. Otherwise, number your files so you will always have one that works (and another one that works less, and so forth back all the way)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.