在Google Colab上,使用:
! shell-command
非常慢。你知道吗
下面是一个测试:
import os
%timeit os.system('date > /dev/null')
%timeit ! date > /dev/null
提供输出:
100 loops, best of 3: 8.58 ms per loop
1 loop, best of 3: 1.56 s per loop
这使得对一个简单命令使用! command
比使用system()
慢180倍。你知道吗
如何避免使用! command
,同时仍将stdout/stderr实时写入输出单元?你知道吗
特别是,我希望能够在单个屏幕行上显示wget
的动态进度条,但我会满足于为每个进度条更新写一行新行的解决方案。你知道吗
Python Run External Command And Get Output On Screen or In Variable给出:
请注意,这不会捕获或打印STDERR。欢迎编辑。你知道吗
你可以试试
subprocess.check_output
。它的工作原理类似于!
,但是您需要首先将命令拆分为数组。你知道吗相关问题 更多 >
编程相关推荐