This is my code, If the file name exists it will ask the user if they would like to overwrite or not if they do not the code is sorted but I am struggling to find where it allow me overwrite to the excel file that is already in existance.
import os
filename = str(input("Please enter a file name\n"))
print(TableModel)
file_exists = False
while file_exists == False:
if os.path.isfile(filename):
file_exists = True
overwrite = str(input("File name is in existance. Would you like to overwrite this yes. Y for yes, N for no\n"))
if overwrite == "N" or overwrite == "n":
print ("You have chosen not to overwrite this file")
filename = str(input("Please enter a different file name\n"))
elif overwrite == "y" or overwrite == "y":
file_exists = True
f = open(filename, 'w')
text = f.read()
text = re.sub('foobar', 'bar', text)
f.seek(0)
f.write(text)
f.truncate()
f.close()