小编Gop*_*i B的帖子

嗨,当我运行以下代码时,我得到的计数为0,为什么,我希望它显示文件中的行数

文件中有1910行,但是当我尝试打印行数时,我的值为0,为什么?已经打开了文件句柄,仅当我在count变量获得正确值后再次打开文件句柄时,这为什么

fhandle=open('C:\\Users\\Gopi\\Documents\\Exercise Files\\mbox-short.txt','r')

for i in fhandle:
    print(i)
#counting lines in a file
count=0
#fhandle=open('C:\\Users\\Gopi\\Documents\\Exercise Files\\mbox-short.txt','r')
for j in fhandle:
    count=count+1
print('Number of lines in the file is',count)
Run Code Online (Sandbox Code Playgroud)

实际结果0预期结果1910

python filehandle python-3.x

0
推荐指数
1
解决办法
37
查看次数

标签 统计

filehandle ×1

python ×1

python-3.x ×1