是否有一种方法可以计算控制台中印刷线的数量,而无需一些变量"手动"在每个输入或输出中增加?
我想计算迄今为止在终端窗口上打印的行数。
例如:
import os
os.system("clear")
# os.system("cls") on windows
for i in range(10):
print("Line: ", i)
# function that prints some more lines and I don't know the number
printSomeMoreLines()
# call some function here to return the number of lines printed in
# the terminal window so far
print(someFunctionToReturnTheNumberOfLinesPrintedInTheTerminalSoFar())
类似的东西可能会起作用(使用管道和带有行数的dipe count命令, wc -l
):
python -c "import sys; print('n'.join(sys.path))" | wc -l
这给我的系统提供了10
...
显然,这是一个Unix命令。对于Windows,您可以尝试这样的事情:
https://superuser.com/questions/959036/what-is-the-windows-equivalent-of-wc-l#959037