diff --git a/Python_Open-Read-&-Count-Text-File.py b/Python_Open-Read-&-Count-Text-File.py index 6177b5f..6179300 100644 --- a/Python_Open-Read-&-Count-Text-File.py +++ b/Python_Open-Read-&-Count-Text-File.py @@ -2,17 +2,33 @@ ##Given a file path: ##read file, count total words and lines -##open, read, close file -a = open("hello.txt") +##Pulls/Imports + ##open, read, close file +a = open("hello.txt") ##puth path to your text file content = a.read() a.close() -##count words in file and format output with placeholder and word list count -words = content.split() -print("There are {0} words in the file.".format(len(words))) +##Definitions: + ##count words in file and format output with placeholder and word list count +def wordcount(a): + words = content.split() + print("There are {0} words in the file.".format(len(words))) -##count lines in file and format output with placeholder and line list count -num_lines = sum(1 for line in open("hello.txt")) -print("There are {0} lines in the file.".format(num_lines)) + ##count lines in file and format output with placeholder and line list count +def linecount(a): + num_lines = sum(1 for line in open("hello.txt")) + print("There are {0} lines in the file.".format(num_lines)) + ##count characters in file +def count_characters(content): + words = content.split() + counter = 0 + for i in words: + counter = counter + len(i) + print("There are {0} characters in the file.".format(counter)) + +##Calls +wordcount(a) +linecount(a) +count_characters(content)