-2

[continuation]- second returns the result as zero, why and how do make both read functions work with the same file name?

CODE:

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

   **#number of lines in the file:**

r2=myfile.readlines()

nol=len(r2)

print('The number of lines in trust.txt is',nol)

**#size of the file:**

r1=myfile.read()

sof=len(r1)

print('The size of trust.txt is',sof,'bytes')

myfile.close()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Rayyan
  • 1
  • 1

1 Answers1

0

Either rewind file pointer before reading it again using myfile.seek(0)

Or better use

with open('file.txt') as f:
    read_data = f.read()
# file is closed now
rtxndr
  • 872
  • 1
  • 9
  • 20