Download our e-book of Introduction To Python
Shashank Shanu
2 years ago
file_name = 'python.txt'
with open(file_name) as fn:
# Read each line
line = fn.readline()
# Keep count of lines
line_count = 1
while line:
print("Line {}: {}".format(line_count, line.strip()))
line = fn.readline()
line_count += 1
Line 1: Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales.
Line 2: Python features a dynamic type system and automatic memory management. It supports multiple programming paradigms, including object-oriented, imperative, functional and procedural, and has a large and comprehensive standard library.
from collections import Counter
with open(r'my_file.txt') as f:
freq = Counter(f.read().split())
print(freq)
Counter({'and': 4, 'Python': 3, 'that': 2, 'a': 2, 'programming': 3, 'code': 1, '1991,': 1, 'is': 1, 'programming.': 1, 'dynamic': 3, 'an': 1, 'design': 1, 'in': 1, 'high-level': 1, 'management.': 1, 'features': 1, 'readability,': 1, 'van': 1, 'both': 1, 'for': 1, 'Rossum': 1, 'system': 1, 'provides': 1, 'memory': 1, 'has': 1, 'type': 1, 'enable': 1, 'Created': 1, 'philosophy': 5, 'constructs': 1, 'emphasizes': 1, 'general-purpose': 1, 'notably': 5, 'released': 1, 'significant': 1, 'Guido': 1, 'using': 1, 'interpreted': 1, 'by': 1, 'on': 1, 'language': 1, 'whitespace.': 1, 'clear': 1, 'It': 1, 'large': 1, 'small': 1, 'automatic': 1, 'scales.': 1, 'first': 2})