-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathfunctions.py
66 lines (54 loc) · 2.35 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
import os
def create_file(title,content):
"""
This function creates a text file in the 'created-notes' directory with the given title and content.
If the directory doesn't exist, it will be created.
After creating the file, it prompts the user to print the content in the terminal.
"""
directory = 'N/notes-app/created-notes/'
filename = title.lower() + ".txt"
final_content = title.upper() + "\n\n" + content
# Ensuring that the directory exists and if it doesn't, creating it
if not os.path.exists(directory):
os.makedirs(directory)
# Combining the directory and filename to get the full file path
file_path = os.path.join(directory, filename)
with open(file_path, 'w') as file:
file.write(final_content)
print("\nContent written successfully!! in " + filename+"\n")
to_print = input("SHOULD I PRINT IT IN THE TERMINAL?(Y for yes) ")
if to_print=='Y' or to_print=='y':
with open(file_path, 'r') as file:
printing_content = file.read()
print('\n'+'-'*50+'\n'+' '*20+'YOUR NOTE\n'+'-'*50)
print(printing_content)
def view_note(title):
"""
This function prints the contents of the text file with given title if the file exists.
If file doesn't exists, it prints error message.
"""
filename = title.lower() + '.txt'
directory = 'N/notes-app/created-notes/'
try:
with open(directory+filename, 'r') as file:
print_contents = file.read()
print('\n'+'-'*50+'\n'+' '*20+'YOUR NOTE\n'+'-'*50)
print(print_contents)
except FileNotFoundError:
print('\n'+'-'*50+'\n'+' '*20+'ERROR\n'+'-'*50)
print("No such text file available !!!")
def delete_note(title):
"""
This function deletes the text file with given title if the file exists.
If file doesn't exists, it prints error message.
"""
filename = title.lower() + '.txt'
directory = 'N/notes-app/created-notes/'
file_path = directory + filename
if os.path.exists(file_path):
os.remove(file_path) # Deleting the file
print('\n'+'-'*50+'\n'+' '*20+'DELETED\n'+'-'*50)
print(filename+ "successfully deleted !!!")
else:
print('\n'+'-'*50+'\n'+' '*20+'ERROR\n'+'-'*50)
print("No such text file available !!!")