python subprocess.Popen vs shlex问题

python subprocess.Popen vs shlex问题,python,list,subprocess,pipe,Python,List,Subprocess,Pipe,我的search first off子进程命令只搜索我写的一个目录(s2),省略了第一个目录(s1)。第二,我在阅读python文档时感到困惑 我的代码 def search_entry(self, widget): s1 = subprocess.Popen(['find', '/home/bludiescript/tv-shows', '-type', 'f'], shell=False, stdout=subprocess.PIPE) s2

我的search first off子进程命令只搜索我写的一个目录(s2),省略了第一个目录(s1)。第二,我在阅读python文档时感到困惑

我的代码

def search_entry(self, widget):
            s1 = subprocess.Popen(['find', '/home/bludiescript/tv-shows', '-type', 'f'], shell=False, stdout=subprocess.PIPE)
            s2 = subprocess.Popen(['find', '/media/FreeAgent\ GoFlex\ Drive/tobins-media', '-type', 'f'],  stdin=s1.stdout, shell=False, stdout=subprocess.PIPE)
            s1.stdout.close()
            self.contents = "\n".join(self.list)
            s2.communicate(self.contents)
我所困惑的是shlex模块,以及如何在我的代码中使用它来代替subprocess.Popen,以及它是否有意义

那么,有人会比我更喜欢这份工作吗

cmd = 'find /media/FreeAgent\ GoFlex\ Drive/tobins-media -type f find /home/bludiescript/tv-shows -type f'
 spl = shlex.split(cmd)
 s1 = subprocess.Popen(spl, stdout=subprocess.PIPE)
 self.contents = "\n".join(self.list)
        s1.communicate(self.contents)

再次感谢您的输入

听起来您想运行一对命令并将它们的输出连接起来:

cmds = [
    'find /media/FreeAgent\ GoFlex\ Drive/tobins-media -type f',
    'find /home/bludiescript/tv-shows -type f'
]

ouput = '\n'.join(subprocess.check_output(shlex.split(cmd)) for cmd in cmds)
尝试而不是调用
find
。这将产生更健壮的代码。以下内容相当于您第一次调用
find

top = '/media/FreeAgent GoFlex Drive/tobins-media'
for dirpath, dirnames, filenames in os.walk(top):
    for filename in filenames:
        print os.path.join(dirpath, filename)

但这并不能回答问题。

是的,这就是我所寻找的。我的原始代码不起作用的原因是什么呢?就您最初的尝试而言,类似这样的方法可以单独捕获每个命令的输出:
proc=subprocess.Popen(cmd,stdout=subprocess.PIPE);output=proc.communicate()[0]
。有关其工作原理的更多详细信息,请参阅。同样,它仍然只搜索第一个find entry。我的示例运行这两个命令,以及它们的结果,我不知道这是不是你想要的。我使用find命令的唯一原因是因为我想我可以将搜索栏中的文本条目链接到find命令中的文件名。但这也不起作用,我尝试了os.walk,它确实列出了目录下的所有文件,因此它比find的工作方式更接近