开发者

python subprocess.Popen vs shlex question

my sub process command to search first off it only searches one directory that i wrote (s2) omits the first (s1). second i was doing some reading on python docs and got confused.

my code

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, s开发者_Go百科tdout=subprocess.PIPE)
            s1.stdout.close()
            self.contents = "\n".join(self.list)
            s2.communicate(self.contents)

what i got confused about was with the shlex module and how to use it in place of subprocess.Popen in my code and if it would even make sense.

so would some like this work better than what i have

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)

thanks again for you input


It sounds like you want to run a pair of commands and join the output from them:

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)


Try os.walk instead of invoking find. This will result in more robust code. The following is equivalent to your first invocation of 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)

This doesn't answer the question, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜