diff --git a/main.py b/main.py
index 3415b3b..e8f4cc6 100644
--- a/main.py
+++ b/main.py
@@ -35,9 +35,8 @@ def check_connection():
time.sleep(3)
if not wlan_sta.isconnected():
# inside passwd file, there is a list of WiFi networks (CSV format)
- f = open("passwd.dat")
- data = f.readlines()
- f.close()
+ with open("passwd.dat") as f:
+ data = f.readlines()
# Search WiFis in range
ssids_found = wlan_sta.scan()
diff --git a/networkconfig.py b/networkconfig.py
index 4e2c90e..f70ff06 100644
--- a/networkconfig.py
+++ b/networkconfig.py
@@ -106,17 +106,14 @@ def handle_configure(client, request):
"""
send_response(client, response_footer)
try:
- fo = open("passwd.dat", "r")
- ex_data = fo.read()
- fo.close()
- fo = open("passwd.dat", "w")
+ with open("passwd.dat", "r") as fo:
+ ex_data = fo.read()
ex_data = ssid + ";" + password + "\n" + ex_data
- fo.write(ex_data)
- fo.close()
+ with open("passwd.dat", "w") as fo:
+ fo.write(ex_data)
except:
- fo = open("passwd.dat", "w")
- fo.write(ssid + ";" + password + "\n")
- fo.close()
+ with open("passwd.dat", "w") as fo:
+ fo.write(ssid + ";" + password + "\n")
return True
else:
response_footer = """