Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ChatBot_A6/FileSave.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
55 lines (44 sloc)
2.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This module was written by Benjamin Rowland. | |
def readFile(username): | |
"""Takes string input of username, and load user data as backend string""" | |
try: | |
currentFile = open(username + ".txt") | |
savedData = currentFile.read() | |
return savedData | |
except FileNotFoundError: #only runs if there is no file under inputted name | |
currentFile = open(username + ".txt", "w") | |
createFile = currentFile.write("") | |
userSave = currentFile.write("$user" + username + "#user") #Attaches "initiators" and "terminators" for search functions | |
return "newUser" #Prompt used in if statement after the "readFile" function is called | |
finally: | |
currentFile.close | |
def save(user, filename, data): | |
"""Takes input of the current user, the name of the file they want to save, and what data they are saving as a string. No output, but saves data""" | |
f = open(user + ".txt", "a") | |
f.write("$" + filename + data + "#" + filename) #Attaches "initiators" and "terminators" for search functions to either side of saved data. | |
def read(filename, data): | |
"""Takes input of filename for search, and the data it is searching. Returns found data as string""" | |
with open(filename, "r") as f: | |
userString = f.read() | |
t = userString.split("$" + data) #Splits the data into a temporary list "t" with t[0] containing all data before the "initiator" and t[1] everything after | |
output = t[1].split("#" + data) #Splits the data again at the "terminator" withe output[0] containing the data before teh initiator and output[1] everything after | |
return output[0] #Outputs searched term as string | |
def NewUser(user, userTraits): | |
"""Creates a basic user profile under passed username string""" | |
age = userTraits[0] | |
save(user, "age", age) | |
location = userTraits[1] | |
save(user, "location", location) | |
course = userTraits[2] | |
save(user, "course", course) | |
password = userTraits[3] | |
save(user, "password", password) | |
##currentUserData = readFile(username) #Pulls data from user's file into program memory | |
##if (currentUserData == "newUser"): | |
## print("This user is not in our databases. Your file has now been created. Please answer a few questions") | |
## NewUser(username) | |
##else: | |
## print("Hello " + read("user", currentUserData)) | |
## print("You are " + read("age", currentUserData) + " Years Old") | |
## print("You live in " + read("location", currentUserData)) | |
## print("You're Studying " + read("course", currentUserData)) | |