In this lab, we're going to modify your pirate translator from Tuesday so that it works with files.
Task 1: File Input
We're going change how inputs and outputs work for your pirate translator. To make this easier, we're giving you stencil code that is a clean version of activity 2-5 with today's tasks marked. The functionality of our stencil is the same as activity 2-5, so you can also use your own code, but be sure to rename it so that you don't write over your work from last class and add an input function.
To get an input from the command line while running the program, first import the module sys (for system) with statement
import sys. Imports go at the top of the program, just after your comment header.
When you run python3 in the command line, your Python program can always access the arguments you give python3, which is to say any words that come after it. For example, if you type
python3 activity_2-6.py input.txt into your terminal, your program will be able to see "activity_2-6.py" and "input.txt" These inputs are stored in a list called
sys.argv. We'll go over lists more next class, but for now what you need to know is that we count each word starting with 0. So in our example of
python3 activity_2-6.py input.txt,
sys.argv is "activity_2-6.py", and
sys.argv is "input.txt". Note that since the division between words is defined by spaces, it will be slightly more complicated handling any file names that have spaces in them.
Using this information, in main, save the filename given in the command line to a variable using the following line of code:
filename = sys.argv
Task 2: Error Checking
Before opening a file with the given filename, we need to error check the input string. To check that the file exists, import the module
os.path. In your main function, write a statement to check whether the filename exists using the function
os.path.exists. If that function returns true, you know that your input filename exists. If the function returns false, then a file with the given name doesn't exist your current directory. In that case, you should have your program print some kind of error message and exit using the function
Task 3: Read from a file
We now want to get the contents of the file with this filename. To read the contents of a file, we first open it in reading ("r") mode. If we instead wanted to write to the file, we would open it in write ("w") mode. The method
file.read() will return the contents of an open file as a string. Please note that
file is a keyword in Python, so please don't name your files "file".
Use the following code snippet to read from the file in your input function. If your file is short, you can test whether this is working by simply printing out its contents. But be sure that you know what your input is in that case or you might end up printing out an entire book! We've provided a very short input file that you can use for testing: file1.txt.
with open('filename.whatever', 'r') as input_file:
input_string = input_file.read()
Task 4: Write to a file
In the previous version of this code, you printed out your results. Here we're going to write the results to a file. You can write this output as a separate output function or in your main function.
If you open a file in 'w' mode, Python will create a blank file with that name. If a file with that name already exists, Python will write over it, which will delete whatever information was in the file previously. Be sure to name your output file differently, such as replacing a
.txt ending with
_out.txt. To doubly ensure that you don't write over any existing files, check that the file exists as you did in task 2. If the file does exist, you can either modify the filename or prompt for user input to verify that you want to write over an existing file.
With a text file in write mode,
output_file.write("your text") will write the given text to an open file. Using the open statement from task 3 and the filename you just verified, write your pirate-themed text to a file.
Once you're done, please check off your lab with a TA or share your file with email@example.com by midnight, 3/9.