[Python] Python - Attempting to create a new file in a existing folder but im recieving...

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    My goal is to create a new file in an existing folder and add data to it but I am receiving the filenotfound error because the open function is unable to read my new file. Here is the code below:

    content = ["eat", "drink","sleep"]
    filenames = ["todo1.txt","todo2.txt","todo3.txt"]

    for stuff,filename in zip(content, filenames):
    file = open(f"files\{filename}", "w")
    file.writelines(stuff)


    I am recieving this error:

    C:\Users\verma\PycharmProjects\PythonProject\bonus experiments\bonus5.py:5: SyntaxWarning: invalid escape sequence '\{'
    file = open(f"files\{filename}", "w")
    Traceback (most recent call last):
    File "C:\Users\verma\PycharmProjects\PythonProject\bonus experiments\bonus5.py", line 5, in <module>
    file = open(f"files\{filename}", "w")
    C:\Users\verma\PycharmProjects\PythonProject\bonus experiments\bonus5.py:1: SyntaxWarning: invalid escape sequence '\{'
    content = ["eat", "drink","sleep"]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    FileNotFoundError: [Errno 2] No such file or directory: 'files\\todo1.txt'


    The filenotfound error is occuring due to the double backslash which i have not added but python considers it as an object in the f string. Please help me fix that.

    Continue reading...

Compartilhe esta Página