-
Notifications
You must be signed in to change notification settings - Fork 0
/
journalsUtil.py
38 lines (29 loc) · 1.2 KB
/
journalsUtil.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
from pathlib import Path
from journal import journal
def createJournalFileIfNotExists(username):
journalFile = Path("journals/" + username + ".txt")
if not journalFile.is_file():
file = open("journals/" + username + ".txt", "w")
file.close()
def getJournal(username):
createJournalFileIfNotExists(username)
journalDataList = []
with open('journals/' + username + ".txt", 'r') as f:
while True:
timestamp = f.readline().rstrip()
entry = f.readline().rstrip()
if not entry:
break
currentEntry = journal(timestamp, entry, username)
journalDataList.append(currentEntry)
return journalDataList
def saveJournalEntry(journal):
createJournalFileIfNotExists(journal.username)
with open('journals/' + journal.username + ".txt", "a") as f:
f.write(str(journal.timestamp) + "\n")
f.write(str(journal.entry) + "\n")
def overwriteJournalEntry(journalList):
with open('journals/' + journalList[0].username + ".txt", "w+") as f:
for journalEntry in journalList:
f.write(str(journalEntry.timestamp) + "\n")
f.write(str(journalEntry.entry) + "\n")