0

I'm kind of new to Python and trying to figure something out. I am trying to write a quick script to do parameter testing for a C code that I've written.

The part that is getting to me is when I try to use subprocess.call in order to run my C code. The C code has one argument that is the name of a file that is opened within the code itself.

For example:

filename = myclass.path + myclass.inputname
subprocess.call(["./code", filename])

This will run the code, and it passes the correct filename, but the C code will not read in any of the information from the file at all. If I pass something like:

./code "filename"

to the shell, where "filename" is actually what is printed when I use the print command in python, it works just fine.

Just for the sake of being complete, here are the lines that are relevant within my C code:

in = fopen(argv[1], "r");
fscanf(in, "%s %d %d", variable1, &variable2, &variable3);

Any idea what is happening?

3
  • is "filename" printed with or without the quotes? The shell will get rid of them, but subprocess won't. Commented Jun 13, 2012 at 3:29
  • 2
    Agreeing with @mgilson: what does filename look like? It might be a special character issue or something. (You also might want to use os.path.join instead of myclass.path + myclass.inputname). Commented Jun 13, 2012 at 3:33
  • 1
    Do myclass.path have a trailing slash? You might need to add a slash between the path and the filename. Commented Jun 13, 2012 at 4:29

2 Answers 2

1

Aside from the obvious, that you are not checking the result of fopen (are you using absolute path names?), your fscanf is wrong. Try:

fscanf(in, "%s %d %d", variable1, &variable2, &variable3);

Another possibility is that the file is not in the format that fscanf expects - it is particularly picky about that and can easily crash your program if it is wrong.

Sign up to request clarification or add additional context in comments.

Comments

0

Adding printf("%s\n", argv[1]); to your C code will let you see what it is receiving. Does the filename contain spaces? You might also like to check argv[2] in case it does.

Also, check the return value from fopen(). Is it NULL? (in which case it is failing to open the file).

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.