I dont understand why this happened but I got the answer, instead of using a relative file directory I tried a full file directory which was this: (that solved the problem but I still dont understand why I needed the full directory and not the relative directory because thats what I had learnt.
file = open(rf"C:\Users\verma\PycharmProjects\PythonProject\files\{filename}", "w")