Related questions
Write a Python program, final_q9.py , which takes one command line argument, the name of a file.
final_q9.py will be given a single positive integer n indicating a maximum desired line length.
Your program should change the file in the following way:
• Lines containing n characters or less, not including the new line, should not be changed.
• Lines not containing a space character (' ') should not be changed.
• Lines containing more than n characters with a space in the first n characters,
should have the last space in the first n characters changed to a newline character ('\n').
• Lines containing more than n characters without a space in the first n characters,
should have the first space on the line changed to a newline character ('\n').
The above rules should also be applied to new lines as they are created.
Your program should not print anything to stdout.
The only thing it should do is change the file.
For example:
COMP(2041|9044) 22T2 — 22T2 Final Exam Questions https://cgi.cse.unsw.edu.au/~cs2041/22T2/exam/final/questions
14 of 20 18/08/2022, 1:55 pm
$ echo hello there how are you today >hello.txt
$ ./final_q9.py 80 hello.txt
$ cat hello.txt
hello there how are you today
$ ./final_q9.py 23 hello.txt
$ cat hello.txt
hello there how are you
today
$ ./final_q9.py 12 hello.txt
$ cat hello.txt
hello there
how are you
today
$ ./final_q9.py 6 hello.txt
$ cat hello.txt
hello
there
how
are
you
today
$ cp frost.txt f.txt
$ cat f.txt
I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I --
I took the one less traveled by,
And that has made all the difference.
$ ./final_q9.py 20 f.txt
$ cat f.txt
I shall be telling
this with a sigh
Somewhere ages and
ages hence:
Two roads diverged
in a wood, and I --
I took the one less
traveled by,
And that has made
all the difference.
$ ./final_q9.py 10 f.txt
$ cat f.txt
I shall be
telling
this with
a sigh
Somewhere
ages and
ages
hence:
Two roads
diverged
in a wood,
and I --
I took the
one less
traveled
by,
And that
has made
all the
difference.
Note final_q9.py printed nothing - it changed the file it was given as argument.
Make sure your program does this.
NOTE:
Your program can assume it is given one argument which is the name of a file.
COMP(2041|9044) 22T2 — 22T2 Final Exam Questions https://cgi.cse.unsw.edu.au/~cs2041/22T2/exam/final/questions
15 of 20 18/08/2022, 1:55 pm
Your program can assume the file exists.
You can assume the files contains only ASCII bytes.
You can assume the file is small enough to fit in memory, e.g. the lines can be read into a list.
Your program should print nothing to stdout.
You are permitted to create temporary files.
Your answer must be Python only.
You may use any standard Python modules.
Unless they are otherwise disallowed by the following restrictions.
You may not use Shell , C , Perl , or any other language.
You may not run external programs, e.g. via the subprocess module, or any other method.
No error checking is necessary.
When you think your program is working, you can run some simple automated tests:
$ 2041 autotest final_q9
When you are finished working on this activity, you must submit your work by running give:
$ give cs2041 final_q9 final_q9.py
To verify your submissions for this activity:
$ 2041 classrun -check final_q9
Step by stepSolved in 4 steps with 3 images
- Write a python program that prompts the user for their favorite basketball team. It should be able to read the list of teams provided below in a file called favorite_teams.txt and check if their team is in that file. Teams in the file:JazzBullsMavericksSpursIf the team is in the file let the user know that their team is in the list of favorites. If the team is not in the file, add the team to the end of the file. Also, let the user know that their team will be added to the file.Sample Run in File:What is your favorite NBA team? Jazz [Enter]Your team Jazz is in the listFile before and after run: JazzBullsMavericksSpurs Sample Run not in File:What is your favorite NBA team? Pelicans [Enter]Your team Pelicans is not in the list. It will be added.File before run: JazzBullsMavericksSpursFile after run:JazzBullsMavericksSpursPelicansarrow_forwardWrite a function in python named "read_words" that declares a parameter for a filename and returns a collection of unique words in the file. Even though words.txt contains a single word per line, you should not assume that this is true of every file. For example, if the line is a sentence, you should split the line into individual words before storing each word in your collection. Handle any errors that occur by printing a detailed error message. Hint: the Python set works like Java's HashSet.arrow_forwardWrite a Python program that reads a text file called student.txt. The input text file contains student_ID, student_name, Quiz_grade as well as Test_grade for each student. Your program should read each student details from the file Then calculate the average of Quizzes and Tests marks Display the students’ details: Total number of students, total average, the total highest and lowest marks. The program should output the report into a file called output.txt. A sample of output text file is given below.arrow_forward
- here is a text file containing the details of several invoices details. Each invoice uses 3 lines of the file. The first of the three lines is a string giving the date of the invoice, for example "1/5/2021" or "2/6/2021". The second of the three lines contains double giving the amount, the third line is boolean indicating whether it is paid or not. However, the very first line of the file is an integer number, which says how many invoices are given in the file in the lines which follow it (i.e. how many records will follow). Text file example for two vehicles "invoices.txt" 2 1/5/2021 50.0 true 2/6/2021 20.0 false Write code for a method named processTextFile() which will open the file named ‘invoices.txt’, from which it will read the data of invoices. It will create Invoice objects using this data by calling the constructor that takes the parameters (date, amount, paid) , placing them into an ArrayList invoiceList.arrow_forwardWrite a C program that reads a text file "My_book.txt" and counts the number of characters, words, and lines in the file. Then, the program should write these counts to a new file "My_book_info.txt" in the following format: Number of characters: [count] Number of words: [count] Number of lines: [count]arrow_forwardSuppose a csv file contains three comma-separated values (strings) on each line (see the leftcolumn of the example table below). Assume, each line of values are, respectively, thewidth, height, and depth of a box. Write a program called box.py and add thefollowing functions to this program.• read_file() takes a csv filename as a parameter that contains the widths, heights, anddepths of all boxes. This function must read the content of the file, store these values ina 2-D list as shown in the right column of the example table below, and return this list.All strings must be converted to integers before they are stored in the list. Example:csv data 2-D list[[15, 6, 3],[3, 7, 4],[6, 9, 3],[15, 6, 11],[6, 5, 5],[13, 10, 9],[9, 10, 3],[14, 5, 4],[4, 6, 11],[12, 10, 9]]• Add another function called max_volume() to this program that takes this 2-D list as aninput parameter. Each item in that list contains 3 integers representing the dimensions ofa box (i.e., width, height, depth). This...arrow_forward
- Given the following json file, create a Python program that reads the file and outputs an XML file that groups car data appropriately. { "cars": [ { "make": "Honda", "model": "Accord", "color": "White", "engine": "I4", "transmission": "Automatic", "price": 22756.0 }, { "make": "Chevrolet", "model": "Bolt EV", "color": "Gray", "engine": "Not Specified", "transmission": "Automatic", "price": 39551.0 }, { "make": "Dodge", "model": "Challenger", "color": "Gray", "engine": "V6", "transmission": "Automatic", "price": 32216.0 } ]}arrow_forwardurgent plzarrow_forwardWrite a program that queries information from three files(given to you). The first file contains the names and telephone numbers of a group of people. The second file contains the names and Social Security numbers of a group of people. The third file contains the names and annual income of a group of people. The groups of people should overlap. Your program should ask the user for a telephone number and then print the name, Social Security number, and annual income, if it can determine that information. Sample run1: Enter the phone number (7 digits, with a dash): 555-1234 555-1234 is associated with Bob Bob's SSN is 000300021 Bob's salary is 55000 Sample run2: Enter the phone number (7 digits, with a dash): 675-4566 Couldn't find a name associated with that number. Sample run3: Enter the phone number (7 digits, with a dash): 000-2345 000-2345 is associated with John John's SSN is 000000004 John's salary is 65000 python languagearrow_forward
- In Python.arrow_forwardWrite a Python program for a simple quiz game. The questions and answers are stored in a text file named "questions.txt." Each line in the file represents a question and its corresponding options and correct answer in the format: "Question, Option1, Option2, Option3, Option4, CorrectOption." The program should load the questions from the file, present them to the user one by one, and keep track of their score. Allow the user to input their answer, and after completing the quiz, display their final score. Make sure to handle cases where "questions.txt" does not exist or has incorrect formatting. Use functions to structure your code, making it easier to understand and maintain. You can use a list or dictionary to store the questions and options. Ex: questions.txt: What is the capital of France?, Berlin, London, Paris, Madrid, 3 Who is the author of "To Kill a Mockingbird"?, Mark Twain, Harper Lee, J.K. Rowling, Ernest Hemingway, 2 What is the largest mammal on Earth?, Elephant, Blue...arrow_forwardWrite a script file to play a simple number guessing game as follows. The script should generatea random integer in the range 1, 2, 3, . . ., 14, 15. It should provide for the player to makerepeated guesses of the number, and it should indicate if the player has won or give the player ahint after each wrong guess. The responses and hints are as follows:• "You won" and then stop the game.• "Very close" if the guess is within 1 of the correct number.• "Getting close" if the guess is within 2 or 3 of the correct number.• "Not close" if the guess is not within 3 of the correct number.arrow_forward
- Text book imageComputer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONText book imageComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceText book imageNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Text book imageConcepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningText book imagePrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationText book imageSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY