【发布时间】:2024-01-10 20:16:01
【问题描述】:
我正在尝试使用命令提示符获取文件的行数。但是,当我执行代码时,我得到 0 的输出。
import sys
file = open(sys.argv[1],"r")
lines=0
line=file.readline()
while line !="":
lines=lines+1
line=file.readline()
print("Lines:"+str(lines)+"\n")
file.close()
这段代码工作得很好,文件中的行被删除了。导致输出为 0
【问题讨论】:
-
代码有效,但as the OP mentioned in the comments,“数据内的行被删除”。
标签: python string loops file command-prompt