1

This is my first question, any feedback would be amazing.

I am calling a C program from Python3 and the string captured by my variable prints out the newline characters instead of processing a newline character.

Want this: hello world

I get: b'hello\nworld'

I have this c program called find.c:

    int main(int argc, char *argv[]){

        printf("%s:\n%s\n\n",argv[1],argv[2]);
        return 0;
    }

Executed/Ran/Processed by Python3:

    import sys
    import subprocess

    user = sys.argv[1]
    item = sys.argv[2]

    out = subprocess.check_output(["./find", user, item])
    print(out)

Once again, the string "out" contains the "\n" instead of treating it like a new line.

I've tried adding: universal_newlines=True and end="" to check_out() with no luck.

I've tried using Popen() and call() and they both still keep the "\n".

As a last note: I am outputting this string to a textarea in html. And yes I know the b'' is not actually part of the string.

Cheers!

3 Answers 3

1

EHHHH, I figured it out. For anyone interested I did the following:

    print(subprocess.check_output(["./find",user,item],universal_newlines=True))

I didn't actually try using universal_newlines=True exclusively.

Look at that my first accepted answer as well!

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

Comments

0

In your out you need to strip the carriage return and new lines.

import sys
import subprocess

user = sys.argv[1]
item = sys.argv[2]

out = check_output(["./find", user, item]).strip()
print(out)

1 Comment

Hey, thanks for the quick response! However, there was no output in the textarea html, I did however solve it. See my answer, cheers!
0

try this. subprocess is returning a byte string, you want to print it as ascii characters.

print(out.decode('ascii'))

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.