Week 10 Lab Exercise - Hsu Users Web Pages

ADVERTISEMENT

CIS 130 - Intro to Programming
p. 1
Week 10 Lab Exercise - 03-28-07
CIS 130 - Intro to Programming
Week 10 Lab - Wednesday, 03-28-07
Week 10 Lab Exercise
Purpose: get more practice with arrays/lists and file input/output
YOU MAY WORK IN PAIRS FOR THIS LAB. Make sure you understand all the concepts!
NAME(S) ___________________________________________________________________________
CHECK YOUR ANSWERS with at least one other student/pair of students. (This does NOT include the person
you worked with, if you worked in a pair.) Discuss any differences in your answers until you are both
satisfied with what the answers should be (and you may change your answers as a result).
Who you checked your answers with: _________________________________________________
Answer the following in the space provided:
1.
There is a file named numbers.txt in the current working directory. You want to open it for reading, storing
the file returned in a variable infile. Write a Python statement below that will accomplish this.
____________________________________________________________________________
2.
You want to read a line of this file into a variable next_line. Write a Python statement below that will
accomplish this.
____________________________________________________________________________
3.
You want to close this file (you are finished reading from it). Write a Python statement below that will
accomplish this.
____________________________________________________________________________
4.
But, next_line contains a lovely value - we want to write it to another file, stuff_read.txt. Write a Python
statement below that will open this file for writing, deleting any existing contents and storing the file
returned in a variable outfile.
____________________________________________________________________________
5.
Now write the value next_line to this file.
____________________________________________________________________________
6.
And, write a Python statement that will now close this file.
____________________________________________________________________________
7.
Several other things get done, and now you want to add something to stuff_read.txt --- BUT you don't want
to delete its current contents. Write a Python statement below that will open this file for appending, NOT
deleting the existing contents and storing the file returned in a variable apfile.
____________________________________________________________________________

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 3