I want to add a function for user to change file. I am not really sure on how. I have already created a function in the end but its not getting me anywhere.
This is my code:
import string
import re
filename = "phil.txt"
def countLines():
Read number of lines in the text file.
numOflines = 0
with open(filename) as file:
for line in file:
if line != "\n":
numOflines += 1
print("Total number of lines is: ", numOflines, "\n")
return numOflines
def countWords():
"""
count number of words in the text file.
"""
numOfwords = []
count = 0
with open(filename, "r") as file:
content = file.read()
con1 = content.replace("-", " ")
newContent = con1.lower()
words = newContent.strip(",")
wordz = words.split()
for word in wordz:
if word not in numOfwords:
numOfwords += word
count += 1
print("Total number of words is: ", count)
return numOfwords
def countCharacters():
"""
count number of characters in the textfile.
"""
numOfwords = 0
with open(filename, "r") as file:
content = file.read()
newContent = content.replace(".", "")
con1 = re.sub("[^A-Za-z0-9]", "", newContent)
con2 = ''.join(con1.split())
for line in con2:
if line == " ":
continue
else:
numOfwords += 1
print("Total number of characters are: ", numOfwords)
def word_frequency():
fhand = open('phil.txt')
counts = dict()
for line in fhand:
line = line.translate(str.maketrans('', '', string.punctuation))
line = line.lower()
words = line.split()
for word in words:
if word not in counts:
counts[word] = 1
else:
counts[word] += 1
lst = list()
for key, val in list(counts.items()):
lst.append((val, key))
lst.sort(reverse=True)
total = sum(counts.values())
for key, val in lst[:7]:
sign = "%"
sign2 = "|"
roundOff = round(float(key) / float(total) * 100, 1)
print('"{}": {}'.format(val, key), sign2 + str(roundOff) + sign)
def check_letter_frequency():
fhand = open('phil.txt')
letter_frequency_dict = dict()
for wordz in fhand:
wordz = wordz.translate(str.maketrans("", "", string.punctuation))
wordz = wordz.lower()
words = wordz.strip(",")
words = wordz.replace(" ", "")
for letter in words:
if letter in letter_frequency_dict:
letter_frequency_dict[letter] += 1
else:
letter_frequency_dict[letter] = 1
sorted_letter_frequency = [(key, letter_frequency_dict[key]) for key in sorted(letter_frequency_dict, key=letter_frequency_dict.get, reverse=True)]
print("letter frequency of:")
letters = []
from string import ascii_lowercase
total = sum(letter_frequency_dict.values())
for key, value in sorted_letter_frequency[:7]:
letters.append(key)
roundOff = round(float(value) / float(total) * 100, 1)
sign = "%"
sign2 = "|"
print('"{}" : {}'.format(key, value), sign2 + str(roundOff) + sign)
def DoItAll():
countLines()
countWords()
countCharacters()
word_frequency()
check_letter_frequency()
def changeFile(file):
new = input("Input name of file")
with open(file) as newfile:
newFile = newfile.read()
countLines(file)
countWords(file)
countCharacters(file)
word_frequency(file)
check_letter_frequency(file)
return file
Imran_28 0 Newbie Poster
Reverend Jim 4,968 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
Imran_28 commented: I want to make a new function and the functions job should be to change file. +0
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.