write a program to display the no. of lines in the file and size of a file in byte in python
Answers
Answered by
1
Language:
Python
Program:
file=open("File.txt", 'r')
reader=file.readlines()
lines_count =0
word_count=0
for lines in reader:
lines_count+=1
for word in lines:
word_count+=1
print(f"There are {lines_count} lines and {word_count/8} byte data in {file.name}" )
Explanation
- readlines() method prints the lines in the life and we count is using as line counter and words are counted in word counter though a nested loop.
- word count is same as the size of date in file in bits.
- We finally convert that bit into bytes by dividing it by 8.
Similar questions
English,
6 months ago
Math,
6 months ago
Physics,
6 months ago
Math,
1 year ago
English,
1 year ago
Computer Science,
1 year ago
Accountancy,
1 year ago