i have a php file calls a script and prints the output like this
$output=shell_exec('/usr/bin/python hello.py');
echo $output;
it prints;
b'total 16\ndrwx---r-x 2 oae users 4096 Jul 31 14:21 .\ndrwxr-x--x+ 9 oae root 4096 Jul 26 13:59 ..\n-rwx---r-x 1 oae users 90 Aug 3 11:22 hello.py\n-rwx---r-x 1 oae users 225 Aug 3 11:22 index.php\n'
but it should be like this;
total 16K drwx---r-x 2 oae users 4.0K Jul 31 14:21 ./ drwxr-x--x+ 9 oae root 4.0K Jul 26 13:59 ../ -rwx---r-x 1 oae users 90 Aug 3 11:22 hello.py* -rwx---r-x 1 oae users 225 Aug 3 11:22 index.php*
\n characters shouldn't be shown.How can i solve this?
-
ok. Problem was python script. I think i should fix this problem from script. The pre methods are working if i use shell_exec('ls -la ./').Alperen Elhan– Alperen Elhan2012年08月03日 09:47:33 +00:00Commented Aug 3, 2012 at 9:47
4 Answers 4
this can be work
$output=shell_exec('/usr/bin/python hello.py');
echo "<pre>";
print_r($output);
echo "</pre>";
Comments
Hope you are trying to print this string in web.
In that case, PHP may not work with \n characters instead it expects to be <br/>
PHP has an inbuilt function which takes care of this transformation of strings.
1 Comment
An alternative would be wrapping the string between <pre>...</pre> tags.
Comments
It seems like an output from a Python 3 script:
from subprocess import check_output as qx
print(qx(['ls']))
The subprocess returns bytes and the print function prints their representation as b'...\n...' where '\n' are two characters (not a newline).
You could fix it by converting qx() returned value to Unicode and then printing it or by writing the bytes as is:
sys.stdout.buffer.write(qx(['ls']))