-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment08_TEith.py
64 lines (51 loc) · 2.34 KB
/
Assignment08_TEith.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# --------------------------------------------------------------#
# Title: Working with Classes
# Dev: TEith
# Date: May 20, 2018
# ChangeLog: (Who, When, What)
# TEith, 05/20/2018, Modified assignment 8 template to include
# adding a class
# NOTE: Products.txt must exist for this to run
# --------------------------------------------------------------#
# Clear the screen for a fresh look
print("\033[H\033[J")
# Class
class FileHelper(object):
strUserInput = None # A string which holds user input
def write_product_user_input(self, file): # Method for looping data entry
try:
print("Type in a Product Id, Name, and Price you want to add to the file")
print("(Enter 'Exit' to quit!)")
while True:
self.strUserInput = input("Enter the Id, Name, and Price (ex. 1,ProductA,9.99): ") # Collect input
if self.strUserInput.lower() == "exit": # Allow for program exit
break
else:
file.write(self.strUserInput + "\n") # Write contents of strUserInput to file
except Exception as err: # Error handling
print("Error: " + str(err))
@staticmethod
def read_all_file_data(file, message="Contents of File"):
try:
print(message) # Print contents read in from myFileHelper.read_all_file_data
file.seek(0) # Start at the beginning of txt file
print(file.read()) # Print it out
except Exception as err: # Error handling
print("Error: " + str(err))
# Data
objFile = None # File Handle
# I/O
myFileHelper = FileHelper() # Class I/0 set to variable
try:
objFile = open("Products.txt", "r+") # Set the File object
myFileHelper.read_all_file_data(objFile, "Here is the current data:") # Reads data from read_all_file_data method
myFileHelper.write_product_user_input(objFile) # Write File with data from write_product_user_input method
myFileHelper.read_all_file_data(objFile, "Here is this data was saved:") # Reread data
except FileNotFoundError as e:
print("Error: " + str(e) + "\n Please check the file name") # Did the file Products.txt get created?
except Exception as e:
print("Error: " + str(e)) # All other errors
finally:
if objFile is not None:
objFile.close() # Close the file