Question
help with python Input file jabberwocky.txt Right click to save this file to a work folder. You will create the script for this lab in
help with python
Input file
jabberwocky.txt
Right click to save this file to a work folder. You will create the script for this lab in the same folder. It probably makes the most sense to create a new folder for this lab. That way you can compare your output file to the jabber.txt that you downloaded for the lab last week.
The contents of the jabberwocky.txt file are as follows:
Jabberwocky by Lewis Carroll (from Through the Looking-Glass and What Alice Found There, 1872)
`Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe.
"Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!"
He took his vorpal sword in hand: Long time the manxome foe he sought -- So rested he by the Tumtum tree, And stood awhile in thought.
And, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whiffling through the tulgey wood, And burbled as it came!
One, two! One, two! And through and through The vorpal blade went snicker-snack! He left it dead, and with its head He went galumphing back.
"And, has thou slain the Jabberwock? Come to my arms, my beamish boy! O frabjous day! Callooh! Callay!' He chortled in his joy.
`Twas brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe.
Basic Overview
The basic structure for this lab is to read through the file and write out the text of the poem, one word per line.
There are a few things to note about the input file:
- The first three lines give the title, author, and source of the poem. They are not part of the poem, proper. Note: This program should work for other poems as well. So, remove three lines, not some given number of characters or words.
- There are blank lines within the poem to break it into four-line strophes.
- Alternate lines of the poem are indented. This mirrors the rhyme scheme of the poem.
Please make sure that you name the output file jabber-out.txt.
The input and output file names should not include drive or folder information, just the simple names:jabberwocky.txt and jabber-out.txt.
Check Level
For the check level, we will modify this program to accept the name of the input and output files from the command line. For the check level, the program will expect two command-line arguments: first, the name of the input file, and second, the name of the output file. If no command-line parameters are given, it will use the names: jabberwocky.txt and jabber-out.txt
Hint: You may find it helpful to create a function that takes two parameters, the input file name and the output filename, and have main call this function based on the values of the command-line arguments.
For example, say our script was named MakeJabber.py. To get the original functionality, we could run the script at the command-line, either with command-line arguments:
$ python MakeJabber.py jabberwocky.txt jabber-out.txt
or without:
$ python MakeJabber.py
In these two example, the dollar sign ($) represents the command-line prompt.
On the other hand, the following command line will create jabber.txt, instead of jabber-out.txt:
$ python MakeJabber.py jabberwocky.txt jabber.txt
Here are some additional input files that you can use to test your script:
- poem1.txt
- poem2.txt
- poem3.txt
- poem4.txt
Notice that the first three lines are simply placeholders in these poems. That is, the name, poet, and source are not given. [participation fodder: In the forum for this week, provide one piece of information about one of these poems. That is, just the name or poet or some comment (it doesn't have to be the source) for one of the poems. It is very important that you share the opportunity for participation points. If you give more than one piece of information, you will not get participation credit. The fodder post should give the filename and one piece of information.]
Plus Level
For the plus level, we have two enhancements:
- error handling
- multiple files
Error Handling
This enhancement will handle files which cannot be opened. If either the input file or the output cannot be opened (for reading or writing, respectively, obviously), the script should not crash, but rather print out an error message and complete normally.
Multiple Files
This enhancement will check the number of command-line arguments.
If there are no command-line arguments, the default files will be used, jabberwocky.txt and jabber-out.txt.
If there is one command-line argument or three or more, the command-line arguments will be understood to be input files. The script will "generate" output file names by adding four characters ('-out') before the file extension of the input file. For example, the input file name poem1.txt would generate the output filename poem1-out.txt.
If there are exactly two command-line arguments, the script will check to see if the second argument exists as file. If the second argument is the name of a file that already exists, both command-line arguments will be understood as input filenames. If the second is not the name of the file that already exists, the second argument will be understood as the output filename.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started