By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,630 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,630 IT Pros & Developers. It's quick & easy.

passing argument to script

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
> 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

P: n/a
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

P: n/a
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.