You're getting the error because \ in strings can create escape sequences. Fix it by using a raw string or double backslashes:
file = open(f"files\\{filename}", "w")
or
file = open(rf"files\{filename}", "w")
Also, ensure the files folder actually exists before running the code.