pygtk OSError: [Errno 2] 没有这样的文件或目录。subprocess.Popen PIPE命令

0 投票
2 回答
6079 浏览
提问于 2025-04-17 02:56

我刚开始学习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)

我的搜索框:

self.search = gtk.Entry()
            self.search.connect("activate", self.search_entry,)
            self.box1.pack_start(self.search, True, True, 0)
            self.search.show()

错误信息:

File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

2 个回答

0

你是说在第二行找吗?看起来它在找文件“fine”时出错了。

2

把所有参数分开,放在args列表里:

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)

我这边的输出

>>> import subprocess
>>> s1 = subprocess.Popen(['find /home/bludiescript/tv-shows', '-type f'], shell=False, stdout=subprocess.PIPE)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 672, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1201, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
>>> s1 = subprocess.Popen(['find','/home/bludiescript/tv-shows', '-type','f'], shell=False, stdout=subprocess.PIPE)
>>> find: `/home/bludiescript/tv-shows': No such file or directory

第一个是你原来的代码,它会引发一个Python错误。第二个代码运行得没问题,但“find”会抱怨,因为我系统里没有“bludiescript/tv-shows”这个目录。

撰写回答