Contents
- file_read2.py
- file_read3.py
- file_read.py
- file_search2.py
- file_search3.py
- file_search.py
- file_write.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, 10.17.2007
FILENAME="data/years.dat"
# opens 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, 10.17.2007
FILENAME="data/years.dat"
# opens the file in "read" mode
dataFile = file(FILENAME, "r")
#dataFile = file("data/years.dat", "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, 10.17.2007
FILENAME="data/years.dat"
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_search2.py 4/7
[top][prev][next]
# Opens a file, reads the file one line at a time,
# searching for data from the user.
# Ignores lines that begin with "#" as comments.
# by Sara Sprenkle, 10.17.2007
# Make a constant so easy to change if what signifies a commented line
# changes.
COMMENT = "#"
inputfile = "data/years2.dat"
searchFor = raw_input("What are you looking for? ")
# opens the file in "read" mode
dataFile = file(inputfile, "r")
numOccurences = 0
# reads in the file and prints the content of the file
for line in dataFile:
# removes the trailing and beginning whitespace
line = line.strip()
# skip the commented lines
if not line.startswith(COMMENT):
# look for the line
if searchFor in line:
print "I found", searchFor, "in line \""+ line + "\""
numOccurences+=1
# close the file with the method "close"
dataFile.close()
print searchFor, "was found", numOccurences, "times"
file_search3.py 5/7
[top][prev][next]
#
# by Sara Sprenkle, 10.17.2007
# Constants representing the different years
FRESHMAN = "FR"
SOPHOMORE = "SO"
JUNIOR = "JR"
SENIOR = "SR"
def main() :
infilename = raw_input("What class file do you want to search? ")
outfilename = raw_input("What is the name of your summary output file? ")
outFile = file(outfilename, "w")
outFile.write("# Summary of number of each year in the class\n")
fr_count = search_file(infilename, FRESHMAN)
so_count = search_file(infilename, SOPHOMORE)
jr_count = search_file(infilename, JUNIOR)
sr_count = search_file(infilename, SENIOR)
outFile.write("%s %d\n" % (FRESHMAN, fr_count) )
outFile.write("%s %d\n" % (SOPHOMORE, so_count) )
outFile.write("%s %d\n" % (JUNIOR, jr_count) )
outFile.write("%s %d\n" % (SENIOR, sr_count) )
outFile.close()
def search_file(filename, searchFor):
# opens the file in "read" mode
dataFile = file(filename, "r")
numOccurences = 0
# reads in the file and prints the content of the file
for line in dataFile:
# removes the trailing and beginning whitespace
line = line.strip()
# skip the commented lines
if not line.startswith(COMMENT) :
# look for the line
if searchFor in line:
# print "I found", searchFor, "in line \""+ line + "\""
numOccurences+=1
# close the file with the method "close"
dataFile.close()
return numOccurences
main()
file_search.py 6/7
[top][prev][next]
# Opens a file, reads the file one line at a time,
# searching for data from the user
# by Sara Sprenkle, 10.17.2007
FILENAME="data/years.dat"
searchFor = raw_input("What are you looking for? ")
# opens the file in "read" mode
dataFile = file(FILENAME, "r")
# reads in the file and prints the content of the file
for line in dataFile:
# strip is a string method that
# removes the trailing and beginning whitespace
line = line.strip()
# look for the line
if searchFor in line:
print "I found", searchFor, "in line \""+ line + "\""
# close the file with the method "close"
dataFile.close()
file_write.py 7/7
[top][prev][next]
# Writes content from a user to a file
# by Sara Sprenkle, 10.17.2007
PROMPT = "What do you want to add to the file? (Nothing will exit): "
outfilename = raw_input("What is the name of your output file? ")
# opens the file in "write" mode
dataFile = file(outfilename, "w")
while True:
userinput = raw_input(PROMPT)
if userinput == "" :
break
# write the user's input to the file
dataFile.write(userinput)
# write a newline after each input from the user
dataFile.write("\n")
# Example of how to write numeric data to the file
#dataFile.write(str(45))
# close the file with the method "close"
dataFile.close()
Generated by GNU enscript 1.6.4.