CS8 Programming Assignment 4
DescriptionIn this programming assignment, you will write programs that read from text files, and write into them. The basic python routines for opening files for reading and writing are:
mf1 = open("file1.txt","r") mf2 = open("file2.txt","w")The above commands assume that the current working directory Python uses contains the text file "file1.txt". The second file need not exist, and at the execution of the "open" command an empty file will be created. If the second file file2.txt exists prior to the command mf2 = open("file2.txt","w"), then its content will be destroyed (i.e., it will become an empty file).
The variables "mf1" and "mf2" are logical links to your text files within Python's execution environment. To read the first line from mf1, you may run
s = mf1.readline()This command reads the first line as a string and assigns to the string variable s. If you enter s to the Python prompt, you will see the line you read from the file followed by a newline character "\n", such as 'aardvark\n'. If you run the same command the second time
t = mf1.readline()Python reads the second line from the file mf1 and assigns it the string t. File reading and writing is a sequential operation. The next time "mf1.readline()" is executed the third line will be read, and so on.
A faster way to read from a file is in a "for" loop:
for aline in mf1: Statement1 Statement2assuming the Statement1 or Statement2 uses the string "aline" in some way.
Similarly a string can be written into the file mf2 ("file2.txt") which was created for the purpose of writing. You can write to a file in several different ways, such as
mf2.write("This is the first line\n") s = "This is the second line\n" mf2.write(s) x = 123 t = str(x) mf2.write(t)In the last one, string "123" is written into the file (without the EOL character). If you wish to add an EOL character, you may use
x = 123 t = str(x) t = t + "\n" mf2.write(t)
After all reading and writing operations, the opened files need to be closed.
mf1.close() mf2.close()Closing a file is particularly necessary for files written into, otherwise they will remain empty files. For more information, refer to Python documentation for input and output: url
RequirementsI am providing a text file containing 58,112 words. You can download using the link: words.txt using the right-click "Save Link As". Place this file into the directory where you will have your pa4.py program. This way your code can easily find the text file with no extra work on your end. This is for your testing purposes and you do not need to submit words.txt. We already have a copy!
What to SubmitFunctions to be submitted:
getListBegin(c,ifile,ofile) getListEnd(c,ifile,ofile) getListContain(s,ifile,ofile) getListRhyme(s,ifile,ofile) getListCount(n,ifile,ofile)
Write all 5 functions in a Python text file named pa4.py, and submit your file using the Dropbox link. Submit only the functions; no test programs outside the functions are needed. Also, you may write and include in your pa4.py file as many helper functions as you want. Furthermore, your functions do not print or return any value; they read from the input file ifile and write into output file ofile. While you are testing your functions, you can open files for reading and writing, however, these 5 functions do not open files. They receive links to the files that are already opened. For example, a typical test program (on my laptop) will look like below, assuming the file words.txt is located in my Desktop:
import os os.chdir("/Users/koc/Desktop") mf1 = open("words.txt","r") mf2 = open("nlist.txt","w") getListCount(4,mf1,mf2) mf1.close() mf2.close()The function getListCount does not open or close the files. It only reads from mf1 and writes to mf2. The test program opens the file words.txt (which is located in my Desktop) and creates the logical link mf1 to this file, and lets the function getListCount to read from it by passing mf1 to the function. Similarly, the output file nlist.txt is created on my Desktop by the test program.
This programming assignment is graded out of 15 points, i.e., it has 5 points of extra credit.
Important: Add your full name and email address as a
comment in the first line of your file, such as:
cs8 web page