Python FileNotFoundError [Errno 2]

49 Views Asked by At

I am newbie to python coding and need your expertise help in my below issue, Currently I am trying to read data from a txt file and process further once its moved to a list.

Below is my code:


case 'add':
            todo = input("enter a todo")+"\n"
            file = open('data.txt','r')
            todos =file.readlines()
            file.close()
            todos.append(todo)
            file = open('data.txt','w')
            file.writelines(todos)
            file.close()

Error I'm getting (please note both my main.py and data.txt are in the same folder)

 file = open('data.txt','r')
           ^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'data.txt'
1

There are 1 best solutions below

0
Ferhat Aytug On

You should include the full path to where data.txt is located if it's not in the same folder;
Example

open("D:\Text\data.txt","w+")