111 lab4

Computer Science 111 – Fundamentals of Programming I Programming Project 4
Due date: 11:59 PM Friday 9 October

In this project you will develop some scripts for processing files.

Warmup

Create a project4 directory. Copy the file testfile.txt from the course webpage to this directory. Launch idle3 and run the following Python statements in a shell window:

inputFile = open("testfil.txt", "r")
inputFile = open("testfile.txt", "r")
text = inputFile.read()
text
print(text)
inputFile.close()
inputFile = open("testfile.txt", "r")
for line in inputFile:
    print(line)
    print(len(line))
    print(line.split())
# Note the intentional misspelling!
outputFile = open("numbers.txt", "w")
for number in range(1, 101): outputFile.write(str(number) + "\n")
outputFile.close()
inputFile = open("numbers.txt", "r")
theSum = 0
for line in inputFile: theSum += int(line)
theSum

To Turn in

You will add four new files named filestats.py (Exercise 1), randomintegers.py (Exercise 2),analyzeintegers.py (Exercise 3), and numberlines.py (for Exercise 4) to your current project directory. Use a text editor to check your file outputs.

  1. Write a script that prompts the user for a file name and outputs the number of characters, words, and lines in the file. If the file does not exist, the program simply prints an error message. Hint: you should need only one loop to solve this problem.
  2. Write a script that outputs random integers to a text file. (Review last week’s lab for a refresher on how to generate random numbers.) The inputs are the file name and the number of integers. The program should output random integers from 1 through the input number. You may use a space or a newline as the separator between integers in the file.
  3. Write a script that reads integers from a file and displays the number of them, their average, their maximum value and their minimum value. Be sure to label your outputs.
  4. Write a script that numbers the lines of text in a file. The line numbers should be right justified in a width of four columns, with two spaces separating each line number and the following text. The script should create a new file with a new file name for the output. For example, if the input file name is testfile.txt, the output file name could benumberedtestfile.txt.

Remember to include a prefatory docstring for each script.

Drop your project directory into the course turnin directory.