Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python can't find file

I am having great difficulty getting python 3.4 to recognize a path or text file on a windows 8 system. I have tried a variety of different approaches but get the similar errors (which likely implies something simple regarding the 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 simplest means to access the file (at least that I know of) is f=open

These lines were coded 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'
like image 317
user4434674 Avatar asked Jan 08 '15 21:01

user4434674


1 Answers

This is an old question, but thought I'd throw my two cents in anyway.

I had this same issue. I was using VS Code for my IDE, and had the folder setting to a folder above where I had the file. This obviously was the issue. Once I opened the folder in VScode where the code and the text file were both located I was able to open the file with no issues.

like image 105
Fletchius Avatar answered Sep 29 '22 15:09

Fletchius