I'm trying to remove a file that physically exists on my system. I wrote the following function using Python 3 that does not work:
def remove_file(output_file):
print("Removing the output file: ", output_file)
try:
os.remove(output_file)
except RemoveFileError as e:
remove_stat = e.returncode
else:
remove_stat = 1
if remove_stat == 0:
print("File removed!")
else:
print("File not removed.")
When I try to remove the file using the above code this is the result I get:
Removing the output file: ../../../output_files/aws_instance_list/aws-master-list-03-21-2019.csv
File not removed.
What am I doing wrong?
remove_stat = 1toremove_stat = 0.tryandexceptblock to show the real error. only writeos.remove(output_file)to see what is the real errorremove_statto1in theelse, which is executed when no exception is raised.remove_stateto1upon success, but then you check that it's equal to0to see if it was successful? When would that condition ever happen?