Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Popen的意外输出_Python_Subprocess_Popen - Fatal编程技术网

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)