#!/usr/bin/python
# Filename: codi.py
################ COmpany DIrectory ################
while True:
ui = raw_input('#> ') # ui for user input
if ui in ('quit', 'exit'):
print 'Goodbye.'
break
if ui == 'help':
print 'Options: help, search, add, remove, quit'
continue
if ui == 'add':
coname = raw_input('Type in company name: ')
phnumber = raw_input('Type in company phone number: ')
addin = coname + ' ' + phnumber
myList = addin
f = open('codilist.txt', 'a') # open for 'w'riting. 'r' is for reading. 'a' is for append.
f.write('\n' + myList) # write user input to list
f.close() # close the file
continue
if ui == 'remove':
coname = raw_input('What company do you want to remove? ') # company name
f = open('codilist.txt', 'r') # original phone number listing
f1 = open('codilist.tmp', 'a') # open a tmp file
for line in f: # THIS IS WHERE I NEED HELP <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
if line.strip() == coname.strip(): # a match! don't add this line, but add the rest of the file
for line in f:
# write remaining lines
f1.write(line)
break # WILL LATER OVERWRITE THE codilist.txt WITH THE TMP FILE
else:
f1.write(line)
else:
print 'Error: That company is not listed.'
f1.close()
f.close()
continue
if ui == 'search':
usearch = raw_input('What company do you want to look for? ') # user search
with open('codilist.txt') as f:
line = f.readline()
for line in f:
if usearch in line:
print line
break
else:
print 'Company name is not listed.'
f.close() # close the file
continue