SyntaxError: Non-ASCII character '\xff' when running C executable using subprocess
SyntaxError: Non-ASCII character '\xff' when running C executable using subprocess
我需要 运行 一个程序,并通过使用 subprocess 模块在 python 脚本中使用它的控制台输出。
这是我的代码:
#!/usr/bin/python
# coding=utf-8
from __future__ import unicode_literals
import sys
from subprocess import Popen, PIPE, STDOUT
# Check the code by running linux list command
p = Popen(["ls", "-l"], stdout=PIPE, bufsize=1)
with p.stdout:
for line in iter(p.stdout.readline, b''):
print line
p.wait() # wait for the subprocess to exit
# Run the C hello world program
p = Popen([sys.executable, "hello_C"], stdout=PIPE, bufsize=1)
with p.stdout:
for line in iter(p.stdout.readline, b''):
print line
p.wait() # wait for the subprocess to exite
"ls -l" 输出显示在 Python 控制台中没有问题。我假设与子进程和读取标准输出相关的代码是正确的。但是当我尝试 运行 hello_C 程序时,它只是一个 hello world 程序,它给出了以下错误:
File "hello_C", line 1
SyntaxError: Non-ASCII character '\xff' in file hello_C on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
sys.executable
是 Python 解释器。 Sys hello_C
是编译好的 C 程序,不是 Python 脚本,你不应该用它来 运行 程序。直接运行程序即可。
p = Popen(["./hello_C"], stdout=PIPE, bufsize=1)
我需要 运行 一个程序,并通过使用 subprocess 模块在 python 脚本中使用它的控制台输出。 这是我的代码:
#!/usr/bin/python
# coding=utf-8
from __future__ import unicode_literals
import sys
from subprocess import Popen, PIPE, STDOUT
# Check the code by running linux list command
p = Popen(["ls", "-l"], stdout=PIPE, bufsize=1)
with p.stdout:
for line in iter(p.stdout.readline, b''):
print line
p.wait() # wait for the subprocess to exit
# Run the C hello world program
p = Popen([sys.executable, "hello_C"], stdout=PIPE, bufsize=1)
with p.stdout:
for line in iter(p.stdout.readline, b''):
print line
p.wait() # wait for the subprocess to exite
"ls -l" 输出显示在 Python 控制台中没有问题。我假设与子进程和读取标准输出相关的代码是正确的。但是当我尝试 运行 hello_C 程序时,它只是一个 hello world 程序,它给出了以下错误:
File "hello_C", line 1
SyntaxError: Non-ASCII character '\xff' in file hello_C on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
sys.executable
是 Python 解释器。 Sys hello_C
是编译好的 C 程序,不是 Python 脚本,你不应该用它来 运行 程序。直接运行程序即可。
p = Popen(["./hello_C"], stdout=PIPE, bufsize=1)