PRIYANSHUDHAKED commited on
Commit
216f34f
·
verified ·
1 Parent(s): 948f7c1

Upload record_attendance.py

Browse files
Files changed (1) hide show
  1. record_attendance.py +58 -0
record_attendance.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import os
3
+ from datetime import datetime
4
+
5
+ def record_attendance_new(present_students: list, subject: str):
6
+ # Automatically get the current date in YYYY-MM-DD format
7
+ date = datetime.now().strftime("%Y-%m-%d")
8
+
9
+ # Create or open the CSV file based on the subject
10
+ csv_file_path = f'{subject}.csv'
11
+
12
+ # Check if the file exists
13
+ if not os.path.exists(csv_file_path):
14
+ # If the file doesn't exist, create it and add the date as the first column header
15
+ with open(csv_file_path, 'w', newline='') as csvfile:
16
+ writer = csv.writer(csvfile)
17
+ writer.writerow(['Roll Number', date]) # Initialize with Roll Number as the first column
18
+ for roll_number in present_students:
19
+ writer.writerow([roll_number]) # Insert the roll numbers under the date column
20
+ else:
21
+ # If the file exists, open it and check the structure
22
+ # Read the existing CSV file
23
+ with open(csv_file_path, 'r', newline='') as csvfile:
24
+ reader = csv.reader(csvfile)
25
+ rows = list(reader)
26
+
27
+ # Check if the date column already exists
28
+ if date not in rows[0]:
29
+ # Add a new date column if it doesn't exist
30
+ rows[0].append(date)
31
+
32
+ # Collect the index of the date column
33
+ date_col_index = rows[0].index(date)
34
+
35
+ # Update or add roll numbers for the given date
36
+ for roll_number in present_students:
37
+ # Check if the roll number already exists in the file
38
+ found = False
39
+ for row in rows[1:]:
40
+ if row[0] == str(roll_number): # Roll number matches
41
+ found = True
42
+ # If the roll number is found and the corresponding date cell is empty, fill it
43
+ if len(row) < date_col_index + 1: # Extend the row if it doesn't have enough columns
44
+ row.extend([''] * (date_col_index - len(row) + 1))
45
+ if row[date_col_index] == '':
46
+ row[date_col_index] = 'Present'
47
+ break
48
+ if not found:
49
+ # If the roll number is not found, add a new row for this roll number
50
+ new_row = [str(roll_number)] + [''] * (date_col_index - 1) + ['Present']
51
+ rows.append(new_row)
52
+
53
+ # Write the updated data back to the CSV file
54
+ with open(csv_file_path, 'w', newline='') as csvfile:
55
+ writer = csv.writer(csvfile)
56
+ writer.writerows(rows)
57
+
58
+ print(f"Attendance recorded for {len(present_students)} students on {date} in {subject}.")