Python cannot find file

It is very difficult for me to get python 3.4 to recognize a path or a text file on Windows 8. I have tried different approaches, but I get similar errors (which probably implies something simple in terms of syntax).

The file itself is located in the same folder as the script file trying to open it: C: \ Users \ User \ Desktop \ Python stuff \ Data.txt

for simplicity, the easiest way to access a file (at least I know) f=open

These lines were encoded as:

f = open("Data.txt", "r")

and

f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r") 

but return the error:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\Python stuff\Testscript.py", line 3, in <module>
    f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/User/Desktop/Python stuff/Data.txt'
+4
source share
2 answers
  • for f = open("Data.txt", "r")

Make sure your .py and .txt files are in the same directory.

  • for f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")

I think space in Python stuffis a mess.

: . , .

>>> [i for i in open('/Users/pk-msrb/projects/temp/temp es/temp.txt')]
['1\n', '2\n', '3\n', '\n']
+1

, "data.txt" , "data.txt.txt", "data.txt" , .

"data.txt" , .

+1

Source: https://habr.com/ru/post/1570515/


All Articles