Open main menu

CDOT Wiki β

Changes

OPS245 Lab 4

23 bytes added, 07:54, 14 October 2021
m
Using argparse to Obtain Positional Arguments from the Command Line
<ol><li>You will be using your '''c7host''' machine for this section.</li>
<li>Change to your '''bin''' directory.</li>
<li>Use your '''tarchiver.py command ''' (from lab 3) as a command to make a tar archive of /tmp called mytmp.tar.<br />
You'll notice that even after hitting enter to run the command, you still needed to give more data to your script (to tell it which directory you wanted to archive, what to call it, and what compression to use). <br />
Requiring this much interaction from the user means that this script is not very good for automation. We can't schedule this script to automatically run, because we (or another admin) need to be present to type answers to the prompts.</li>
<li>Make a copy of your tarchiver.py script and call it '''tarchiver2.py'''. We will work with tarchiver2.py for the rest of this investigation.</li>
<li>Import the argparse module into tarchiver2.py.</li>
<li>Add the following lines to your script, after the import, but before you prompt the user for anything:<br />
<code>destination = args.dest</code><br />
Note: Instead of '''destination''', use the variable name were already using to store the value you were getting from the user. That way you won't have to change it in the rest of your script.</li>
<li>Try using your script to make another archived copy of /tmp, this time calling it '''secondtmp.tar'''.<br />
If you didn't provide secondtmp.tar on the command line when you ran the command, you'll notice that your script complained. Try running:<br />
<code>tarchiver.py secondtmp.tar</code></li>
572
edits