I'm working on a project that requires me to open CSV files. It must check that the file is open and then return it to main. I've written a function to do so and would like to know how it looks, if I've written it well or how I can improve it:
menu.py
def print_menu_prompt():
"""
Prints a menu prompt to the user
Returns
Nothing - just a prompt.
"""
print("\n-----------------------------------------------------------------------------------")
print("\nPlease enter the file path for the CSV you wish to calculate the hours worked for.")
print("\nIn file explorer, navigate to the location of the CSV, click once on the file and copy the file path.")
print("\nPaste the file path in exactly as it is - if the file path is incorrect then the program will not run.")
print("\n-----------------------------------------------------------------------------------")
open_csv.py
from modules import menu
def open_file_prompt():
"""
Display the prompt for the user to specify the path for the CSV file that they wish to open in the program.
Returns
The CSV file
"""
# Infinite loop to open the CSV file, if file not found then it will loop again until the file is found.
while True:
menu.print_menu_prompt()
timesheet_csv = input("\nPlease paste the path in now: ")
# Try and except to check if the CSV file has opened successfully, if so then it is returned to main.
try:
with open(timesheet_csv, 'r') as file:
if not file.closed:
print("\nThe CSV file opened successfully")
return timesheet_csv
break
except FileNotFoundError:
print(f"\nERROR: The specified file does not exist.")
except IOError:
print("\nAn error occured while trying to open the CSV file.")