I am a new python learner and I was trying to make a simple program as a practice. However, there's a problem. The if statement in this code gets executed even if it is false. I left a comment near the faulty if statement or basically it is at line 25.
I tried to put the if statement in try as an intended block but nothing changed.
Code:
def errormessage(asr, cnum):
print("Process terminated due to a error.\nError: " + str(asr) + "\nType: " + asr.__class__.__name__ + "\nCodeNum: " + str(cnum))
quit()
modelist = ("r", "w", "a")
fname = input("Enter file name or directory: ")
fmode = input("Enter file mode (r/w/a): ")
if fmode not in modelist:
print("Invalid file mode, quitting...")
quit()
else:
pass
try:
if fmode == "r":
fop = open(fname, fmode)
print("File content:\n\n" + fop.read())
elif fmode == "w" or "a":
pass
except Exception as err:
errormessage(err, 1)
if fmode == "w" or "a": # FAULTY IF STATEMENT
try:
fwriteinp = str(input("What do you want to write/append to the file? (type abort to abort): "))
if fwriteinp == "abort":
print("Process aborted.")
quit()
else:
pass
fwrite = open(fname, fmode)
fwrite.write(str(fwriteinp))
fwrite.close()
print(str(open(fname, "r").read()))
except Exception as err:
errormessage(err, 2)
else:
pass
if fname.endswith(".py"):
try:
execopt = input("Do you want to execute the file? (y/n): ")
try:
if execopt == "y":
exec(open(fname).read())
else:
print("Quitting...")
quit()
except Exception as err:
errormessage(err, 3)
except Exception as err:
errormessage(err, 4)
else:
quit()
Help is appreciated.