Write a Python code to find the size of the file in bytes, the number of lines, number of words and no. of character.
Source code
import os
lines = 0
words = 0
letters = 0
filesize = 0
for line in open("Mydoc.txt"):
lines += 1
letters += len(line)
# get the size of file
filesize = os.path.getsize("Mydoc.txt")
# A flag that signals the location outside the word.
pos = 'out'
for letter in line:
if letter != ' ' and pos == 'out':
words += 1
pos = 'in'
elif letter == ' ':
pos = 'out'
print("Size of File is",filesize,'bytes')
print("Lines:", lines)
print("Words:", words)
print("Letters:", letters)
Sample snapshot
Search Console
- Write a Python code to find the size of the file in bytes, the number of lines, number of words and no. of character.
- Python program
- Computer Science 083
- Class 12 CBSE Term 2 practical exam