Question

Python Please, If possible, please continue with/ use code already given

File Commands: u/a Write a function named, file_commands, that takes the name of a file as a parameter. The function processe

File Commands: u/a

Write a function named, file_commands, that takes the name of a file as a parameter.

The function processes the contents the file as follows:

  • For file lines that begin with the letter 'a', calculate & print the integer average of the numbers on the line.
  • For file lines that begin with the letter 'u', print the upper case format for each word following the u.

Sample Input File:

u hello world!
a 3 4 5 6

Expected Output:

HELLO WORLD!
4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Implemented the code as per the requirement. As python is tab specific, you may not get the formatted text while copying the code,
so I'm attaching the screenshots of the code for reference. Please make sure when you are executing the below code you have same format, especially tabs.

Please comment if any modification required.

Code:

====

import sys
def file_commands(file_name):
sum=0
count=0
with open(file_name) as file:
for line in file:
if(line.split()[0]=='u'):
for word in line.split()[1:]:
print(word.upper()+" ", end='')
print()
else:
for str_num in line.split()[1:]:
num = int(str_num)
sum = sum+num
count = count+1
print(int(sum/count))
sum=0
count=0

if __name__ == "__main__":
file_commands(sys.argv[1])

code screenshot:

==============

1 import sys 2 def file_commands (file_name): 3 sum=0 4 count=0 5 with open(file_name) as file: 6 for line in file: 7 if(line

output:

=====

HELLO WORLD! 4.

Add a comment
Know the answer?
Add Answer to:
Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...

    C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...

  • 5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename),...

    5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename), starting line number i.e. m) and ending line number (i.e. n) as a parameter. The function then returns the contents from line number m to n (m <n). If m < 1 then start from line 1. Similarly, if n > number of lines the file, then stop at the last line in the file. Sample Input: readlinesmton("data.txt",5,7) Sample Input: readlinesmton("data.txt", 0,10)

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • this us python plz follow the instructions while u solving I need the code and sane...

    this us python plz follow the instructions while u solving I need the code and sane output QUESTION 1 Write a program that performs the following: 1) request a sentence (at least two words with no punctuation marks) input by the user 2) display the first word 3) display the last letter of the first word 4) display the last word 5) display the first letter of the last word input text can be any content just make sure to...

  • C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

    C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.     Your function should be named...

  • Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two...

    Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file exists when wordLengths is called; wordLengths must create the file outFile. The input file contains only letters and white space For each line of the input file, wordLengths should identify the length of the longest word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT