passing argument to script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eight02645999@yahoo.com

    #1

    passing argument to script

    hi

    if i have a string like this

    "ABCE-123456 ABC_DEF_Suggest (abc def ghi).txt"

    that needs to be passed to a python script
    and i wanted to get the words inside the brackets after i passed this
    string. I did a re
    something like

    thestring = sys.argv[1:]
    pat = re.compile(r".* \((.*)\)\.txt$" )
    if pat.search(thes tring):
    words = pat.search(thes tring).group(1)

    but it doesn't return anything for words variable.
    When i specifically define the string inside the python script , it
    works

    thestring = "ABCE-123456 ABC_DEF_Suggest (abc def ghi).txt"

    I also tried str(thestring) but also did not work
    what is wrong with the argument passing?

    thanks

  • Daniel Nogradi

    #2
    Re: passing argument to script

    > hi[color=blue]
    >
    > if i have a string like this
    >
    > "ABCE-123456 ABC_DEF_Suggest (abc def ghi).txt"
    >
    > that needs to be passed to a python script
    > and i wanted to get the words inside the brackets after i passed this
    > string. I did a re
    > something like
    >
    > thestring = sys.argv[1:]
    > pat = re.compile(r".* \((.*)\)\.txt$" )
    > if pat.search(thes tring):
    > words = pat.search(thes tring).group(1)
    >
    > but it doesn't return anything for words variable.
    > When i specifically define the string inside the python script , it
    > works
    >
    > thestring = "ABCE-123456 ABC_DEF_Suggest (abc def ghi).txt"
    >
    > I also tried str(thestring) but also did not work
    > what is wrong with the argument passing?[/color]

    Are you doing this on Linux or Windows?

    If you execute your script from the command line on Linux you need to
    enclose it in quotation marks otherwise your shell will interfere. So
    you need to invoke your program as

    python yourscript.py "ABCE-123456 ABC_DEF_Suggest (abc def ghi).txt"

    and need to refer to the argument as sys.argv[1:][0], so yourscript.py should be

    import sys, re
    thestring = sys.argv[1:][0]
    pat = re.compile(r".* \((.*)\)\.txt$" )
    if pat.search(thes tring):
    words = pat.search(thes tring).group(1)
    print words

    I'm not sure what you need to do on Windows though.

    Comment

    • BartlebyScrivener

      #3
      Re: passing argument to script

      Works for me.

      I get "abc def ghi" using your script on Windows XP and ActiveState
      Python 2.4.3

      rd

      Comment

      • Sion Arrowsmith

        #4
        Re: passing argument to script

        Daniel Nogradi <nogradi@gmail. com> wrote:[color=blue]
        >If you execute your script from the command line on Linux you need to
        >enclose it in quotation marks otherwise your shell will interfere. So
        >you need to invoke your program as
        >
        >python yourscript.py "ABCE-123456 ABC_DEF_Suggest (abc def ghi).txt"[/color]

        Same is true on Windows. It's just that some commands magically convert
        filenames with spaces into a single argument if you don't quote them.
        (Compare, for instance, cd \Program Files with dir \Program Files .)
        [color=blue]
        >and need to refer to the argument as sys.argv[1:][0][/color]

        That's an interesting way of spelling sys.argv[1] .

        --
        \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
        ___ | "Frankly I have no feelings towards penguins one way or the other"
        \X/ | -- Arthur C. Clarke
        her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

        Comment

        Working...