Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -------------------------------------------------#
- # Title: Adding a class structure to a script
- # Dev: Craig Lewin
- # Date: May 20, 2019
- # ChangeLog: (Who, When, What)
- # Craig Lewin, 05/20/2019, Began coding for assignment 8
- # -------------------------------------------------#
- class Product(object):
- """Doc String"""
- # ---------------------------------------#
- # Desc: Manipulates product data
- # Dev: Craig Lewin
- # Date: 5/20/2019
- # ChangeLog: (When, Who, What)
- # - 5/20/2019, CLewin, Wrote class
- # ---------------------------------------#
- # --Fields--
- strUserInput = None # A string which holds user input
- # --Constructor--
- # Attributes
- # --Properties--
- # --Methods--
- def WriteProductUserInput(File):
- try:
- print("Type in a Product Id, Name, and Price you want to add to the file")
- print("(Enter 'Exit' to quit!)")
- while (True):
- strUserInput = input("Enter the Id, Name, and Price (ex. 1,ProductA,9.99): ")
- if (strUserInput.lower() == "exit"):
- break
- else:
- File.write(strUserInput + "\n")
- except Exception as e:
- print("Error: " + str(e))
- # --End of Product class--
- class FileIO(object):
- """Doc String"""
- # ---------------------------------------#
- # Desc: Interacts with file objects for reading/writing
- # Dev: Craig Lewin
- # Date: 5/20/2019
- # ChangeLog: (When, Who, What)
- # - 5/20/2019, CLewin, Wrote class
- # ---------------------------------------#
- # --Fields--
- objFile = None # File Handle
- # --Constructor--
- # Attributes
- # --Properties--
- # --Methods--
- def ReadAllFileData(File, Message="Contents of File"):
- try:
- print(Message)
- File.seek(0)
- print(File.read())
- except Exception as e:
- print("Error: " + str(e))
- # --End of FileIO class--
- try:
- objFile = open("Products.txt", "r+")
- FileIO.ReadAllFileData(objFile, "Here is the current data:")
- Product.WriteProductUserInput(objFile)
- FileIO.ReadAllFileData(objFile, "Here is this data was saved:")
- except FileNotFoundError as e:
- print("Error: " + str(e) + "\n Please check the file name")
- except Exception as e:
- print("Error: " + str(e))
- finally:
- if(objFile != None):objFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement