-1

Python can't find where my .txt file is, how can I find the correct path to it or where do I need to put the file?

I tried the following but got an error:

with open("C:\Users\raynaud\Downloads\notlar.txt","r",encoding="utf-8") as file:

with open("dosya.txt","r",encoding= "utf-8") as file:

FileNotFoundError: [Errno 2] No such file or directory: 'dosya.txt'

liakoyras
  • 1,101
  • 12
  • 27

2 Answers2

1

If you are not using absolute path, you must place the file in the same directory as the script is. On absolute paths you should not use \ like "C:\Users\UserName\", because \ is the escape character in Python (and many more languages). You must use it like this: "C:\\Users\\UserName\\"

CoderCharmander
  • 1,862
  • 10
  • 18
1

Have you checked your current directory? It might be pointing to somewhere unexpected. Try:

import os
os.getcwd()
Plato77
  • 422
  • 3
  • 15