有没有一种方法可以计算控制台中打印的行数,而无需在每次输入或输出时“手动”增加一些变量?
我想计算到目前为止在终端窗口上打印的行数。
例如:
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())
最佳答案
类似的东西可能会起作用(使用管道和带有行数的字数统计命令,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
关于python - 计算控制台Python中打印的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42334290/