-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_handler.py
More file actions
42 lines (28 loc) · 1.04 KB
/
Copy pathdata_handler.py
File metadata and controls
42 lines (28 loc) · 1.04 KB
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
import csv
import os
FILE_NAME = "employees.csv"
def load_data():
file_exists = os.path.exists(FILE_NAME)
if file_exists == False:
empty_list = []
return empty_list
with open(FILE_NAME, mode='r', newline='', encoding='utf-8') as file_object:
reader = csv.DictReader(file_object)
data_list = []
for row in reader:
data_list.append(row)
return data_list
def save_data(employees):
length_of_data = len(employees)
if length_of_data == 0:
return
first_employee = employees[0]
headers = first_employee.keys()
with open(FILE_NAME, mode='w', newline='', encoding='utf-8') as file_object:
writer = csv.DictWriter(file_object, fieldnames=headers)
writer.writeheader()
for emp in employees:
writer.writerow(emp)
def export_report(report_text):
with open("department_report.txt", mode='w', encoding='utf-8') as file_object:
file_object.write(report_text)