在给定的代码中,i所放置的第一个块顺序返回结果,而i所放置的一个块[在desc中继续]

问题描述 投票:0回答:1

[continuation]-秒返回结果为零,为什么以及如何使两个读取函数使用相同的文件名?

CODE:

myfile = open(r'C:\ Users \ win8 \ Documents \ Cs Record \ trust.txt','r')

#文件中的行数:

r2 = myfile.readlines()

nol = len(r2)

print('trust.txt中的行数为',nol)

文件的#大小:

r1 = myfile.read()

sof = len(r1)

print('trust.txt的大小是',sof,'字节')

myfile.close()

python python-3.x file readlines
1个回答
0
投票

在使用myfile.seek(0)再次读取文件指针之前,倒带文件指针>

或者更好的使用

with open('file.txt') as f:
    read_data = f.read()
# file is closed now
© www.soinside.com 2019 - 2024. All rights reserved.