Reputation: 21
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!
Upvotes: 1
Views: 70
Reputation:
try this. subprocess is returning a byte string, you want to print it as ascii characters.
print(out.decode('ascii'))
Upvotes: 0
Reputation: 21
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!
Upvotes: 1
Reputation: 4821
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)
Upvotes: 0