Contents
- file_read2.py
- file_read3.py
- file_read.py
- file_search.py
- menu.py
- menu_withfunctions.py
- oldmac.py
file_read2.py 1/7
[top][prev][next]
# Opens a file, reads the file one line at a time, and prints the
# contents
# by Sara Sprenkle
FILENAME="data/years.dat"
# creates a new file object, opening the file in "read" mode
dataFile = file(FILENAME, "r")
# reads in the file, line-by-line and prints the content of the file
line = dataFile.readline()
while line != "":
print line,
line = dataFile.readline()
# close the file with the method "close"
dataFile.close()
file_read3.py 2/7
[top][prev][next]
# Opens a file, reads the file one line at a time, and prints the
# contents,
# by Sara Sprenkle
FILENAME="data/years.dat"
# creates a new file object, opening the file in "read" mode
dataFile = file(FILENAME, "r")
# reads in the file line-by-line and prints the content of the file
for line in dataFile:
print line,
# close the file with the method "close"
dataFile.close()
file_read.py 3/7
[top][prev][next]
# Opens a file, reads it, and prints out its contents.
# by Sara Sprenkle
FILENAME="data/years.dat"
# creates a new file object, opening the file in read mode
myFile = file(FILENAME, "r")
# read the file and put it into one string
contents = myFile.read()
# close the file when you're done reading the file
myFile.close()
# print out the contents of the file
print contents,
file_search.py 4/7
[top][prev][next]
# This program allows you to search a file for a term and
# displays which lines of the file contain that term and
# a count of the number of lines that contained that term.
# CSCI 111
FILENAME="data/years.dat"
searchTerm = "SO"
# creates a new file object, opening the file in "read" mode
dataFile = file(FILENAME, "r")
numOccurences = 0
# reads in the file line-by-line and prints the content of the file
for line in dataFile:
print line,
if searchTerm in line:
numOccurences += 1
# close the file with the method "close"
dataFile.close()
print "We found", searchTerm, numOccurences, "times in the file"
menu.py 5/7
[top][prev][next]
STOP_OPTION = 'Q'
def printWelcomeScreen(name):
welcome = "Welcome to " + name + "!"
length = len(welcome)
print length*"-"
print welcome
print length*"-"
def printMenu():
print "You have some options for what to do: "
print "Enter an 'F' to find a song"
print "Enter an 'S' to sort by Song title"
print "Enter an 'A' to sort by Album"
print "Enter an 'R' to sort by aRtist name"
print "Enter an 'H' to list your options again"
print "Enter a 'Q' to quit"
menu_withfunctions.py 6/7
[top][prev][next]
# Using functions from menu module
# by Sara Sprenkle
from menu import *
printWelcomeScreen("MusicManager")
printMenu()
menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()
while menuChoice != STOP_OPTION :
printMenu()
menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()
oldmac.py 7/7
[top][prev][next]
# Print out verses of the song Old MacDonald
# Sara Sprenkle
#
BEGIN_END = "Old McDonald had a farm"
EIEIO = ", E-I-E-I-O"
def main():
# call the verse function to print out a verse
printVerse("dog", "ruff")
printVerse("duck", "quack")
animal_type = "cow"
animal_sound = "moo"
printVerse(animal_type, animal_sound)
# QUESTION: What if called function as
# printVerse("ruff", "dog")
# prints a verse of Old MacDonald, plugging in the animal and sound
# parameters (which are strings), as appropriate.
def printVerse(animal, sound):
print BEGIN_END + EIEIO
print "And on that farm he had a " + animal + EIEIO
print "With a " + sound + ", " + sound + " here"
print "And a " + sound + ", " + sound + " there"
print "Here a", sound
print "There a", sound
print "Everywhere a " + sound + ", " + sound
print BEGIN_END + EIEIO
print
#main()
if __name__ == "__main__":
main()
Generated by GNU enscript 1.6.4.