0

I know there are other posts with same question, but I am unable to run a perl script within my python script referring those posts.

Here is the excerpt from my code:

var1 = "-proj xyz"
var2 = "-c groups"
proc2 = subprocess.Popen(["perl", "some_perl_script.pl", var1, var2], stdout=subprocess.PIPE)
proc2.stdin.write(var1)
proc2.stdin.write(var2)
proc2.stdin.close()
proc2.wait()

Note: I'm supposed to run: ./some_perl_script.pl -c groups -proj xyz

Please let me know what am I doing wrong here? Thanks!

2
  • Possible duplicate of this, although I can't be certain without more details about what your actual problem is. Commented Nov 13, 2014 at 21:26
  • 1
    Why are you sending the contents of var1 and var2 to your Popen command, and writing them to stdin? Commented Nov 13, 2014 at 21:26

1 Answer 1

2

You're passing "-proj xyz" as a single argument, instead of two. In other words, instead of this:

./some_perl_script.pl -c groups -proj xyz

… you're doing the equivalent of this:

./some_perl_script.pl '-c groups' '-proj xyz'

So, when the perl script goes looking to see if any argument is -c, none of them are, because -c groups is not -c.

Of course in some cases you'll get away with this; most argument parsers that treat -cfoo and -c foo the same will also treat '-c foo' the same. But I'm guessing any parser that's looking for arguments like -proj isn't such a case.

So, if you really need these option/argument pairs to each be in a separate variable for some reason, try this:

var1 = ["-proj", "xyz"]
var2 = ["-c", "groups"]
proc2 = subprocess.Popen(["perl", "some_perl_script.pl"] + var1 + var2,
                         stdout=subprocess.PIPE)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.