469,282 Members | 1,734 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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(thestring):
words = pat.search(thestring).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

Apr 7 '06 #1
3 1880
> 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(thestring):
words = pat.search(thestring).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?


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(thestring):
words = pat.search(thestring).group(1)
print words

I'm not sure what you need to do on Windows though.
Apr 7 '06 #2
Works for me.

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

rd

Apr 7 '06 #3
Daniel Nogradi <no*****@gmail.com> wrote:
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"
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 .)
and need to refer to the argument as sys.argv[1:][0]


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

--
\S -- si***@chiark.greenend.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
Apr 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
5 posts views Thread by kyosohma | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.