我想在Python 3中运行以下bash命令:
ls -l
Run Code Online (Sandbox Code Playgroud)
我知道我可以做以下事情:
from subprocess import call
call(['ls', '-l'])
Run Code Online (Sandbox Code Playgroud)
如何将此输出保存到文件中,或将其放入列表或集合中?
[-rw-r--r--] [1] [name] [staff] [426] [14 Jan 21:52] [HelloWorld.class]
[-rw-r--r--@] [1] [name] [staff] [107] [14 Jan 21:51] [HelloWorld.java]
...
etc.
Run Code Online (Sandbox Code Playgroud)
我希望能够直接访问特定信息,然后将其添加到集合中,但我不知道将列出多少项目.
任何提示,片段或示例都会有所帮助.
所以我有一个程序,在"主"过程中我触发一个新的Process对象(我想要的)是从STDIN读取行并将它们附加到Queue对象.
基本上,基本的系统设置是有一个"命令获取"过程,用户将输入命令/查询,我需要将这些查询提供给在不同进程中运行的其他子系统.我的想法是通过其他系统可以读取的多处理队列来共享这些.
我所拥有的(专注于获取命令/查询)基本上是:
def sub_proc(q):
some_str = ""
while True:
some_str = raw_input("> ")
if some_str.lower() == "quit":
return
q.put_nowait(some_str)
if __name__ == "__main__":
q = Queue()
qproc = Process(target=sub_proc, args=(q,))
qproc.start()
qproc.join()
# now at this point q should contain all the strings entered by the user
Run Code Online (Sandbox Code Playgroud)
问题是我得到:
Process Process-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "/home/blah/blah/blah/blah.py", line 325, in sub_proc
some_str = raw_input("> …Run Code Online (Sandbox Code Playgroud)