-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScript.py
More file actions
67 lines (58 loc) · 2.05 KB
/
Script.py
File metadata and controls
67 lines (58 loc) · 2.05 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import re
from datetime import date
base_dir = os.path.dirname(os.path.realpath(__file__))
md_dir="GNE/Mech/Academics/"
curr_dir = os.path.abspath(os.path.join(base_dir, md_dir))
regex = re.compile(r'\d+')
pre = ''
heading=str(input("Enter the Notice Heading:- "))
# Check for the current directory
if os.path.isdir(curr_dir):
total_notices = []
for file in os.listdir(curr_dir):
if file.endswith(".md"):
total_notices.append(file)
if len(total_notices) != 0:
str1 = "".join(total_notices)
file_number_str = regex.findall(str1)
file_number_list = list(map(int, file_number_str))
file_number = max(file_number_list)
md_path = "GNE/Mech/Academics/notice" + str(file_number) + ".md"
path = os.path.abspath(os.path.join(base_dir, md_path))
#print(path)
if os.path.isfile(path):
new_md_path = "GNE/Mech/Academics/notice" + str(file_number + 1) + ".md"
new_path = os.path.abspath(os.path.join(base_dir, new_md_path))
#print(new_path)
file_number+=1
file=open(new_path,"w")
file.write("## " + heading + "\nDate : " + str(date.today()) + " \nNotice Number : " + str(file_number) + "\n" + pre)
file.flush()
file.close()
print("Notice added successfully!")
else:
print("File not found")
# Create new file if no .md file exist
else:
file_number = 1
md_path = "GNE/Mech/Academics/notice" + str(file_number) + ".md"
path = os.path.abspath(os.path.join(base_dir, md_path))
#print(path)
file=open(path,"w")
file.write("## " + heading + "\nDate : " + str(date.today()) + " \nNotice Number : " + str(file_number) + "\n" + pre)
file.flush()
file.close()
print("Notice added successfully!")
# Create new directories if not found
else:
os.makedirs(curr_dir)
file_number = 1
md_path = "GNE/Mech/Academics/notice" + str(file_number) + ".md"
path = os.path.abspath(os.path.join(base_dir, md_path))
#print(path)
file=open(path,"w")
file.write("## " + heading + "\nDate : " + str(date.today()) + " \nNotice Number : " + str(file_number) + "\n" + pre)
file.flush()
file.close()
print("Notice added successfully!")