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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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

 

Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY