-
Notifications
You must be signed in to change notification settings - Fork 20
/
count_file_lines.py
33 lines (26 loc) · 1.02 KB
/
count_file_lines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
################################################################################
#
# Program: Count The Number Of Lines In A File
#
# Description: Example of how to count the number of lines in a file using
# Python.
#
# YouTube Lesson: https://www.youtube.com/watch?v=qjrRf_pXWFQ
#
# Author: Kevin Browne @ https://portfoliocourses.com
#
################################################################################
# returns the number of lines in a file with the given filename
def count_lines(filename):
# open the file with the given filename
with open(filename) as file:
# readlines() method returns a list containing each line of the file
lines = file.readlines()
# find the length of the list using len(), i.e. the # of lines in the file
total_lines = len(lines)
# return the number of lines
return total_lines
# use the count_lines() function to get the number of lines in the file
count = count_lines("file.txt")
# output the total number of lines in the file
print("Total:", count)