Python Popen的意外输出
我使用Python Popen的意外输出,python,subprocess,popen,Python,Subprocess,Popen,我使用subprocess中的Popen构造函数捕获在python脚本中运行的命令的输出: import os from subprocess import Popen, PIPE p = Popen(["my-cli", "ls", "/mypics/"], stdin=PIPE, stdout=PIPE,stderr=PIPE) output, err = p.communicate() print(output) print(output.count('jpg')) 我的目标是将输出
subprocess
中的Popen
构造函数捕获在python脚本中运行的命令的输出:
import os
from subprocess import Popen, PIPE
p = Popen(["my-cli", "ls", "/mypics/"], stdin=PIPE, stdout=PIPE,stderr=PIPE)
output, err = p.communicate()
print(output)
print(output.count('jpg'))
我的目标是将输出文件名保存为字符串数组
但是,当我打印输出时,我注意到脚本没有将文件名保存为字符串,而是将每个文件的每个字节(字母)保存为字符串。因此,打印输出如下所示
f
我
l
e
.
j
p
g
一,
因此,我没有打印一个文件名file.jpg
,而是打印出组成文件名的8个独立字符。但是,直接在终端中运行ls
命令只会按原样逐行列出文件名
我在这个脚本中做错了什么?这里的解决方法是什么?我正在运行Python2.7,如有任何建议,将不胜感激您的Popen数组中的我的cli
是什么。我想在每个字符输出后添加一些新行字符。只需删除my cli
,您就可以使用它了
p = Popen(["ls", "/mypics/"], stdin=PIPE, stdout=PIPE,stderr=PIPE)
我希望这能对您起作用。需要cli命令,因为我正在使用一个名为ls
cli本身的方法
p = Popen(["ls", "/mypics/"], stdin=PIPE, stdout=PIPE,stderr=PIPE)