简体   繁体   English

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

[英]Counting number of printed lines in console Python

Is there a way to count the number of printed lines in the console without some variable that would be "manually" incremented at every input or output?有没有办法计算控制台中打印的行数,而无需在每次输入或输出时“手动”增加一些变量?

I want to count the number of lines printed till now on the terminal window.我想计算到目前为止在终端窗口上打印的行数。

For example:例如:

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())

Something like this might work (using pipe and the word count command with number of lines, wc -l ):像这样的事情可能会起作用(使用管道和带有行数的 word count 命令, wc -l ):

python -c "import sys; print('\\n'.join(sys.path))" | wc -l

This gives 10 for my system...这为我的系统提供了10 ...

Obviously, this is a Unix command.显然,这是一个 Unix 命令。 For Windows, you could try something like this:对于 Windows,您可以尝试这样的操作:

https://superuser.com/questions/959036/what-is-the-windows-equivalent-of-wc-l#959037 https://superuser.com/questions/959036/what-is-the-windows-equivalent-of-wc-l#959037

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM