python - 计算控制台Python中打印的行数

标签 python terminal count console

有没有一种方法可以计算控制台中打印的行数,而无需在每次输入或输出时“手动”增加一些变量?

我想计算到目前为止在终端窗口上打印的行数。

例如:

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/

相关文章:

Python:为什么不能使用 self.variable 作为方法中的可选参数?

python : why use %s instead of calling variables directly

Terminal Mathias 的点文件 .functions

python - 计算标记化网址中的单词数

sql-server-2008 - 计算具有特定列数的内容的行

mysql - SELECT MAX from COUNT,其他行变量不正确

python - SVM 总是收敛吗?

python - 显示部分搜索结果的最佳实践(当它们来自辅助服务器时,一一显示)

ubuntu - Ubuntu 和 Manjaro 之间的终端命令区别

在 ~/.vim/上找不到 OS X 10.8 Mountain Lion 上的 VIM 安装