-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_functions.py
54 lines (47 loc) · 1.42 KB
/
csv_functions.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import csv
import os
header: list = [
"name",
"regno",
"email",
"password",
"phone",
"receiptno",
"uniqid",
"sequence",
"current_question",
]
def check_if_exists_in_directory(file_or_folder_name: str, directory: str = "") -> bool:
current_working_dir = os.getcwd()
try:
if directory:
os.chdir(directory)
return file_or_folder_name in os.listdir()
except FileNotFoundError:
return False
finally:
os.chdir(current_working_dir)
def write_to_csv(data: dict, row, filename: str = "CyberRegistrations.csv"):
global header
file_exists = check_if_exists_in_directory(filename)
with open(filename, "a") as csv_file_obj:
csv_write = csv.writer(csv_file_obj, delimiter=",", lineterminator="\n")
if file_exists:
csv_write.writerow(row)
else:
csv_write.writerow(header)
csv_write.writerow(row)
def check_user_exists_in_csv(
regno: str, uniqid: str, filename: str = "CyberRegistrations.csv"
):
if not check_if_exists_in_directory(filename):
return False
else:
with open(filename, "r") as csv_file_obj:
csv_reader = csv.DictReader(csv_file_obj)
for row in csv_reader:
if regno == row["regno"]:
return True
elif uniqid == row["uniqid"]:
return True
return False