Skip to content

Commit 68721db

Browse files
committed
refactor
1 parent 6791888 commit 68721db

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

exercises/utils/file_util.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Author Steven Yeoh
22
# Copyright (c) 2019. All rights reserved.
33

4-
import os.path
4+
import pathlib
55

66

77
def read_file():
8-
if not os.path.exists("./resources"):
9-
os.mkdir("./resources")
8+
if not pathlib.Path("./resources").exists():
9+
pathlib.Path("./resources").mkdir()
1010

11-
if os.path.exists("./resources/user_acc.txt"):
11+
if pathlib.Path("./resources/user_acc.txt").exists():
1212
return open("./resources/user_acc.txt", "r")
1313
else:
1414
return None
1515

1616

1717
def save_new_user_to_file(username, password):
18-
file = open("./resources/user_acc.txt", "a+")
19-
file.write("username={}:password={}\n".format(username, password))
18+
with open("./resources/user_acc.txt", "a+") as file:
19+
file.write("username={}:password={}\n".format(username, password))

exercises/utils/validate_util.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ def print_errors(errors):
3232

3333

3434
def user_exists(username):
35-
file = read_file()
36-
if file is not None:
37-
for data in file:
38-
if data.split(":")[0].split("=")[1] == username:
39-
return True
40-
return False
35+
with read_file() as file:
36+
if file is not None:
37+
for data in file:
38+
if data.split(":")[0].split("=")[1] == username:
39+
return True
40+
return False
4141
return False

0 commit comments

Comments
 (0)