来自 subprocess.run() 的 CompletedProcess 不是 return 字符串
CompletedProcess from subprocess.run() doesn't return a string
根据 Python 3.5 docs,subprocess.run() returns 一个具有标准输出成员的 CompletedProcess 对象包含 "A bytes sequence, or a string if run() was called with universal_newlines=True." 我只看到一个字节序列和不是字符串,我假设(希望)它等同于文本行。例如,
import pprint
import subprocess
my_data = ""
line_count = 0
proc = subprocess.run(
args = [ 'cat', 'input.txt' ],
universal_newlines = True,
stdout = subprocess.PIPE)
for text_line in proc.stdout:
my_data += text_line
line_count += 1
word_file = open('output.txt', 'w')
pprint.pprint(my_data, word_file)
pprint.pprint(line_count, word_file)
注意:这使用了 Python 3.5 中的一项新功能,在以前的版本中不会 运行。
我是否需要创建自己的行缓冲逻辑,或者有没有办法让 Python 为我做这件事?
您看到的是一个字符串,比较一下:
import subprocess
proc = subprocess.run(
args = [ 'cat', 'input.txt' ],
universal_newlines = False,
stdout = subprocess.PIPE)
print (type(proc.stdout))
class 'bytes'
运行 呼叫 popen.communicate
communicate() returns a tuple (stdout_data, stderr_data). The data
will be bytes or, if universal_newlines was True, strings.
查看 here 以获得更多解释和其他 shell 互动。
proc.stdout
在您的情况下已经是一个字符串,运行 print(type(proc.stdout))
,以确保。它包含所有子进程的输出——subprocess.run()
在子进程死掉之前不会 return。
for text_line in proc.stdout:
不正确:for char in text_string
枚举 Python 中的字符(Unicode 代码点),而不是行。要获取线路,请致电:
lines = result.stdout.splitlines()
如果字符串中有 Unicode 换行符,结果可能与 .split('\n')
不同。
如果你想逐行读取输出(以避免运行ning 长时间运行ning 进程内存不足):
from subrocess import Popen, PIPE
with Popen(command, stdout=PIPE, universal_newlines=True) as process:
for line in process.stdout:
do_something_with(line)
注意:process.stdout
在本例中是一个类文件对象。 Popen()
不等待进程完成 -- Popen()
return 在子进程启动后立即执行。 process
是一个 subprocess.Popen
实例,这里不是 CompletedProcess
。
如果您只需要计算输出中的行数(以 b'\n'
结尾),例如 wc -l
:
from functools import partial
with Popen(command, stdout=PIPE) as process:
read_chunk = partial(process.stdout.read, 1 << 13)
line_count = sum(chunk.count(b'\n') for chunk in iter(read_chunk, b''))
见Why is reading lines from stdin much slower in C++ than Python?
如果您需要在数组中包含 STDOUT 行以更好地操作它们,您只是想念 "Universal newline" 分隔符
来拆分输出
nmap_out = subprocess.run(args = ['nmap', '-T4', '-A', '192.168.1.128'],
universal_newlines = True,
stdout = subprocess.PIPE)
nmap_lines = nmap_out.stdout.splitlines()
print(nmap_lines)
输出为:
['Starting Nmap 7.01 ( https://nmap.org ) at 2016-02-28 12:24 CET', 'Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn', 'Nmap done: 1 IP address (0 hosts up) scanned in 2.37 seconds']
根据 Python 3.5 docs,subprocess.run() returns 一个具有标准输出成员的 CompletedProcess 对象包含 "A bytes sequence, or a string if run() was called with universal_newlines=True." 我只看到一个字节序列和不是字符串,我假设(希望)它等同于文本行。例如,
import pprint
import subprocess
my_data = ""
line_count = 0
proc = subprocess.run(
args = [ 'cat', 'input.txt' ],
universal_newlines = True,
stdout = subprocess.PIPE)
for text_line in proc.stdout:
my_data += text_line
line_count += 1
word_file = open('output.txt', 'w')
pprint.pprint(my_data, word_file)
pprint.pprint(line_count, word_file)
注意:这使用了 Python 3.5 中的一项新功能,在以前的版本中不会 运行。
我是否需要创建自己的行缓冲逻辑,或者有没有办法让 Python 为我做这件事?
您看到的是一个字符串,比较一下:
import subprocess
proc = subprocess.run(
args = [ 'cat', 'input.txt' ],
universal_newlines = False,
stdout = subprocess.PIPE)
print (type(proc.stdout))
class 'bytes'
运行 呼叫 popen.communicate
communicate() returns a tuple (stdout_data, stderr_data). The data will be bytes or, if universal_newlines was True, strings.
查看 here 以获得更多解释和其他 shell 互动。
proc.stdout
在您的情况下已经是一个字符串,运行 print(type(proc.stdout))
,以确保。它包含所有子进程的输出——subprocess.run()
在子进程死掉之前不会 return。
for text_line in proc.stdout:
不正确:for char in text_string
枚举 Python 中的字符(Unicode 代码点),而不是行。要获取线路,请致电:
lines = result.stdout.splitlines()
如果字符串中有 Unicode 换行符,结果可能与 .split('\n')
不同。
如果你想逐行读取输出(以避免运行ning 长时间运行ning 进程内存不足):
from subrocess import Popen, PIPE
with Popen(command, stdout=PIPE, universal_newlines=True) as process:
for line in process.stdout:
do_something_with(line)
注意:process.stdout
在本例中是一个类文件对象。 Popen()
不等待进程完成 -- Popen()
return 在子进程启动后立即执行。 process
是一个 subprocess.Popen
实例,这里不是 CompletedProcess
。
如果您只需要计算输出中的行数(以 b'\n'
结尾),例如 wc -l
:
from functools import partial
with Popen(command, stdout=PIPE) as process:
read_chunk = partial(process.stdout.read, 1 << 13)
line_count = sum(chunk.count(b'\n') for chunk in iter(read_chunk, b''))
见Why is reading lines from stdin much slower in C++ than Python?
如果您需要在数组中包含 STDOUT 行以更好地操作它们,您只是想念 "Universal newline" 分隔符
来拆分输出nmap_out = subprocess.run(args = ['nmap', '-T4', '-A', '192.168.1.128'],
universal_newlines = True,
stdout = subprocess.PIPE)
nmap_lines = nmap_out.stdout.splitlines()
print(nmap_lines)
输出为:
['Starting Nmap 7.01 ( https://nmap.org ) at 2016-02-28 12:24 CET', 'Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn', 'Nmap done: 1 IP address (0 hosts up) scanned in 2.37 seconds']