CS 101 - Introduction to Computing, Spring 2007

Lab 14

Reminder

The program Jython Environment for Students (or JES) is the development environment that we will use for this lab.

In the ACCC Labs that have JES, it can be found by:

  1. Clicking on Start
  2. Then click on All Programs
  3. Then click on Class Applications
  4. Then click on Engineering
  5. Finally click on Jython Environment for Students

Writing Text to a File

From JES, we can write text information out to a file. This will be very useful for the 4th Homework assignment. We first need to open a file which would use the following command:
open (name, usage)

where: name is the name of the file you want to write to
where: usage will inform JES we want to write to the file
where: it will return a file object that we store in a variable.

This is often used as follows:
     filename = pickAFile ()
     fileobj = open (filename, "wt")

TO write to the file we need to combine the file object returned by open( ) with the function write( ) as follows:

     fileobj.write("Hello")
The will place the word Hello in the file that was opened.

Note the different way in which the function is used. There is a dot between the file object and the write( ) function. This format is used for object oriented method calls. Don't worry to much about it now, but if you don't follow the format shown above, it won't work.

One item to note is that the write( ) function/method will always write on the smae line of a file. Thus the code:

     fileobj.write("Hello")
     fileobj.write("How are you?")
     fileobj.write("I am fine")

would appear in the file as:

     HelloHow are you?I am fine
To get the data to write on a new line in the file, we must insert a "backslash n". Such as:
     fileobj.write("Hello\n")
     fileobj.write("How are you?\n")
     fileobj.write("I am fine\n")

This would appear in the file as:

     Hello
     How are you?
     I am fine
Note to print a regular backslash in your code you must list two backslashes and to print a double quote you must list a backslash double quote as follows:
     fileobj.write(" \\ \" ")

would appear in the file as:

      \ "

Lab Assignment 14

Due: Tuesday 5/1/2007 by 11:59 pm

For this assignment, your JES file that contains the above function must Create a file using JES that will:

  1. Contain a comment indicating

  2. Contain a JES function called lab14 () that will write the following information out to a file. Each of these items are to be written on a separate line in the file. You are to prompt the user for the name of the file with pickAFile.
These functions may call other functions that are built-in to JES or that you have written. Note that this is very similar to Lab 2 except that we are writing the information to a file not out to the JES command area.

Submittal of the Lab Assignment

Comments on the ACCC Labs

On the computers in the ACCC Labs there should an H: drive. This drive is actually a networked connection to your own file space maintained by the ACCC. No matter what machine you use or what lab you are in, the H: drive will access the same file space. This means that you can save a file on the H: drive on a computer in one lab and access that same file on a computer in another lab. This can be very helpful. It is suggested that you store your python program files on your H: drive.