Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(ANSWER USING PYTHON 3.6) Write a function named file_stats that takes one strin

ID: 3600116 • Letter: #

Question

(ANSWER USING PYTHON 3.6)

Write a function named file_stats that takes one string parameter (in_file) that is the name of an existing text file. The function file_stats should calculate three statistics about in_file: the number of lines it contains, the number of words and the number of characters, and print the three statistics on separate lines. For example, the following would be correct input and output. (Note: the number of characters may vary slightly between operating systems.)

>>> file_stats('created_equal.txt')
lines 2
words 13
characters 72

Explanation / Answer

Dear Student Providing an Example by using python Programm.

# Write a record

out_file = open("test.txt", "w")

out_file.write("This Text will out file Look at it and see!")

out_file.close()

# Read a document

in_file = open("test.txt", "r")

content = in_file.read()

in_file.close()

print content

The yield and the substance of the document test.txt are:

An outline of document:

Get a document protest with the open capacity.

Read or keep in touch with the document protest

Close it

The initial step is to get a document question. The best approach to do this is to utilize the open capacity. The organization is file_object = open(filename, mode) where file_object is the variable to put the record question, filename is a string with the filename, and mode is "r" to peruse a document or "w" to compose a record . Next the document objects capacities can be called. The two most regular capacities are perused and compose. The compose work adds a string to the finish of the document. The read work peruses the following thing in the record and returns it as a string. In the event that no contention is given it will restore the entire document

Programm for numbes:

def print_numbers(numbers):

print "Phone Numbers:"

for x in numbers.keys():

print "Name:", x, " Number:", numbers[x]

print

def add_number(numbers, name, number):

numbers[name] = number

def lookup_number(numbers, name):

on the off chance that name in numbers:

restore "The number is " + numbers[name]

else:

return name + " was not found"

def remove_number(numbers, name):

on the off chance that name in numbers:

del numbers[name]

else:

print name," was not found"

def load_numbers(numbers, filename):

in_file = open(filename, "r")

for in_line in in_file:

in_line = in_line.rstrip(' ') #Eliminate end of line or enter

name, number = in_line.split(",")

numbers[name] = number

in_file.close()

def save_numbers(numbers, filename):

out_file = open(filename, "w")

for x in numbers.keys():

out_file.write(x + "," + numbers[x] + " ")

out_file.close()

def print_menu():

print '1. Print Phone Numbers'

print '2. Include a Phone Number'

print '3. Expel a Phone Number'

print '4. Query a Phone Number'

print '5. Load numbers'

print '6. Spare numbers'

print '7. Stop'

print

phone_list = {}

menu_choice = 0

print_menu()

while True:

menu_choice = input("Type in a number (1-7): ")

on the off chance that menu_choice == 1:

print_numbers(phone_list)

elif menu_choice == 2:

print "Include Name and Number"

name = raw_input("Name: ")

telephone = raw_input("Number: ")

add_number(phone_list, name, telephone)

elif menu_choice == 3:

print "Evacuate Name and Number"

name = raw_input("Name: ")

remove_number(phone_list, name)

elif menu_choice == 4:

print "Query Number"

name = raw_input("Name: ")

print lookup_number(phone_list, name)

elif menu_choice == 5:

filename = raw_input("Filename to stack: ")

load_numbers(phone_list, filename)

elif menu_choice == 6:

filename = raw_input("Filename to spare: ")

save_numbers(phone_list, filename)

elif menu_choice == 7:

break

else:

print_menu()

print "Farewell"

new portions of this program are:

def load_numbers(numbers, filename):

in_file = open(filename, "r")

while True:

in_line = in_file.readline()

if not in_line:

break

in_line = in_line[:- 1]

name, number = in_line.split(",")

numbers[name] = number

in_file.close()

def save_numbers(numbers, filename):

out_file = open(filename, "w")

for x in numbers.keys():

out_file.write(x + "," + numbers[x] + " ")

out_file.close()

we will take a gander at the spare segment of the program. To begin with it makes a document question with the summon open. Next it experiences and makes a line for each of the telephone numbers with the charge out_file.write(x + "," + numbers[x] + " "). This works out a line that contains the name, a comma, the number and tails it by a newline.

The stacking segment is somewhat more muddled. It begins by getting a record question. At that point it utilizes a while True: circle to continue circling until the point that a break articulation is experienced. Next it gets a line with the line in_line in_file.readline The readline capacity will restore a vacant string when the finish of the record is come to. The if articulation checks for this and breaks out of the while circle when that happens. Obviously if the readline work did not restore the newline toward the finish of the line there would be no real way to tell if a vacant string was a vacant line or the finish of the record so the newline is left in what readline returns. Henceforth we need to dispose of the newline. The line in_line = in_line does this for us by dropping the last character. Next the line name, number in_line.splitparts the line at the comma into a name and a number