passing argument to script

E

eight02645999

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
 
D

Daniel Nogradi

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.
 
S

Sion Arrowsmith

Daniel Nogradi said:
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] .
 
B

BartlebyScrivener

Works for me.

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

rd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top