我的子进程命令先搜索它,它只搜索我编写的一个目录(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)再次感谢您的投入
发布于 2011-09-24 08:00:26
这听起来像是您想要运行一对命令并连接它们的输出:
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)发布于 2011-09-24 09:39:14
尝试使用os.walk而不是调用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)然而,这并没有回答这个问题。
https://stackoverflow.com/questions/7535940
复制相似问题