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

ANSWER IN PYTHON PLEASE!! Write a function fcopy() that takes as input two files

ID: 3677718 • Letter: A

Question

ANSWER IN PYTHON PLEASE!!

Write a function fcopy() that takes as input two files (as strings) and copies the content of the first file into the second.
>>> fcopy('example.txt', 'output.txt')
>>> open('output.txt').read()
'The 3 lines in this file end with the nnew line character. There is a blank line above this line. '


2. Write a function stats() that takes one input argument: the name of a text file. The function should print, on the screen, the numbers of lines, words, and characters in the file;
your function should open the file only once.
>>>stats('example.txt')
line count: 3
word count: 20
character count: 98


3.)Assume that the input file is in the current working directory and write the output file to that directory.

For each line of the input file, the function repeatWords() should write to the output file all of the words that appear more than once on that line. Each word should be lower cased and stripped of leading and trailing punctuation. Each repeated word on a line should be written to the corresponding line of the output file only once, regardless of the number of times the word is repeated.

For example, if the following is the content of the file catInTheHat.txt:

Too wet to go out and too cold to play ball.
So we sat in the house.
We did nothing at all.
So all we could do was to Sit! Sit! Sit! Sit!

The following function call:

inF = 'catInTheHat.txt'
outF = 'catRepWords.txt'
repeatWords(inF, outF)

should create the file ‘catRepWords.txt’ with the content:

too to
sit

Hint: for each line, create a list of repeated words and then write the list (without repetition) to the outfile. Be sure to test your solution with input in which some repeated words on a line are a mixture of upper and lower case, and in which repeated words sometimes are preceded or followed by punctuation.

Explanation / Answer

#!/usr/bin/env python
1)
def fcopy(src,dest):
try:
f = open(src)
f1 = open(dest, 'a')
for line in f.readlines():
f1.write(line)
f1.close()
f.close()
# eg. source or destination doesn't exist
except IOError as e:
print('Error: %s' % e.strerror)
2)

def stats(fname):
num_lines = 0
num_words = 0
num_chars = 0

with open(fname, 'r') as f:
for line in f:
words = line.split()

num_lines += 1
num_words += len(words)
num_chars += len(line)
print num_lines
print num_words
print num_chars
f.close()

3)
def repeatWords(filename_1, filename_2):
infile_1=open(filename_1,'r')
content_1=infile_1.read()
infile_1.close()

import string
new_content=""
for char in content_1:
new_content+=char.strip(string.punctuation)

new_content=new_content.lower()
new_content=new_content.split()

repeated_list=[]

for word in new_content:
if new_content.count(word)>1:
repeated_list.append(word)

new_repeat_list=[]

for item in repeated_list:
while item not in new_repeat_list:
new_repeat_list.append(item)

outfile=open(filename_2,'w')
for repeat in new_repeat_list:
outfile.write(repeat)
outfile.write(' ')
outfile.close()

infile_2=open(filename_2,'r')
content_2=infile_2.read()
infile_2.close()
return content_2

inF = 'catInTheHat.txt'
outF = 'catRepWords.txt'
print(repeatWords(inF, outF))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote