|
| 1 | +import os |
| 2 | + |
| 3 | +def create_file(title,content): |
| 4 | + """ |
| 5 | + This function creates a text file in the 'created-notes' directory with the given title and content. |
| 6 | + If the directory doesn't exist, it will be created. |
| 7 | + After creating the file, it prompts the user to print the content in the terminal. |
| 8 | + """ |
| 9 | + directory = 'N/notes-app/created-notes/' |
| 10 | + filename = title.lower() + ".txt" |
| 11 | + final_content = title.upper() + "\n\n" + content |
| 12 | + |
| 13 | + # Ensuring that the directory exists and if it doesn't, creating it |
| 14 | + if not os.path.exists(directory): |
| 15 | + os.makedirs(directory) |
| 16 | + |
| 17 | + # Combining the directory and filename to get the full file path |
| 18 | + file_path = os.path.join(directory, filename) |
| 19 | + |
| 20 | + with open(file_path, 'w') as file: |
| 21 | + file.write(final_content) |
| 22 | + print("\nContent written successfully!! in " + filename+"\n") |
| 23 | + |
| 24 | + to_print = input("SHOULD I PRINT IT IN THE TERMINAL?(Y for yes) ") |
| 25 | + if to_print=='Y' or to_print=='y': |
| 26 | + with open(file_path, 'r') as file: |
| 27 | + printing_content = file.read() |
| 28 | + print('\n'+'-'*50+'\n'+' '*20+'YOUR NOTE\n'+'-'*50) |
| 29 | + print(printing_content) |
| 30 | + |
| 31 | + |
| 32 | +def view_note(title): |
| 33 | + """ |
| 34 | + This function prints the contents of the text file with given title if the file exists. |
| 35 | + If file doesn't exists, it prints error message. |
| 36 | + """ |
| 37 | + filename = title.lower() + '.txt' |
| 38 | + directory = 'N/notes-app/created-notes/' |
| 39 | + try: |
| 40 | + with open(directory+filename, 'r') as file: |
| 41 | + print_contents = file.read() |
| 42 | + print('\n'+'-'*50+'\n'+' '*20+'YOUR NOTE\n'+'-'*50) |
| 43 | + print(print_contents) |
| 44 | + except FileNotFoundError: |
| 45 | + print('\n'+'-'*50+'\n'+' '*20+'ERROR\n'+'-'*50) |
| 46 | + print("No such text file available !!!") |
| 47 | + |
| 48 | + |
| 49 | +def delete_note(title): |
| 50 | + """ |
| 51 | + This function deletes the text file with given title if the file exists. |
| 52 | + If file doesn't exists, it prints error message. |
| 53 | + """ |
| 54 | + filename = title.lower() + '.txt' |
| 55 | + directory = 'N/notes-app/created-notes/' |
| 56 | + file_path = directory + filename |
| 57 | + if os.path.exists(file_path): |
| 58 | + os.remove(file_path) # Deleting the file |
| 59 | + print('\n'+'-'*50+'\n'+' '*20+'DELETED\n'+'-'*50) |
| 60 | + print(filename+ "successfully deleted !!!") |
| 61 | + |
| 62 | + else: |
| 63 | + print('\n'+'-'*50+'\n'+' '*20+'ERROR\n'+'-'*50) |
| 64 | + print("No such text file available !!!") |
| 65 | + |
| 66 | + |
0 commit comments