Jonathan
Jonathan

Reputation: 385

How to invoke a Perl script that need args from Python

I'm writing my script in Python, and I want to invoke a Perl script from it.

This is the line I want to call:

perl conv_intel_hex_2_verilog.pl arg1 arg2

while arg1 is the input from the Python script and arg2 is a new file that I'm creating.

So far I have this:

def main(argv):
    file  = open("test.hex", "w")
    input = argv[0];
    subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input]);
    file.close();

This runs and does nothing.

And when I'm changing the

subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input]);

to

subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input ,file]);

it doesn't compile...

Upvotes: 0

Views: 1931

Answers (2)

Rob Kennedy
Rob Kennedy

Reputation: 163247

As you've described it, the program you're running expects its second argument to be the name of a file, so that's what you need to give it. In your current code, you're giving it a Python file object, which is not the same as the string name of a file.

subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input, "test.hex"]);

There is no need to create the file object prior to running that code, either. The other program will surely create it for you, and might even balk if it finds that the file already exists.

Upvotes: 4

simbabque
simbabque

Reputation: 54323

You cannot just give it a filehandle (or whatever that is called in Python). You are constructing a call to another program, and that program expects a filename, so pass the filename as a string.

subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input ,"test.hex"]);

You also don't need to open the file first.

Upvotes: 3

Related Questions