user4434674
user4434674

Reputation: 323

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'

Upvotes: 16

Views: 153967

Answers (10)

Fletchius
Fletchius

Reputation: 460

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.

Upvotes: 10

Daniel Alamba
Daniel Alamba

Reputation: 23

Try creating a file using:

f = open("myfile.txt", "x")

Then search your computer for "myfile.txt" which is the file you just created. Wherever the location of the file is, that is where your code is reading from :)

Upvotes: -2

0rfanidis
0rfanidis

Reputation: 1

I had the same problem with pyCharm. I found out that you sometimes have to specify the directory by going to edit config and working directory

Upvotes: 0

ryan muir
ryan muir

Reputation: 21

I had a similar issue when using vs code. If you use the built-in green triangle to run, it seems to be able to run the code but it doesn't know where to look for the file. open an integrated terminal to the correct folder and run from the terminal line. Seems like something vs code could figure out...

Upvotes: 0

structure
structure

Reputation: 55

I know this is an old question, but here is another option.

set your parent_folder if you have multiple files in the same folder:

parent folder = "C:/Users/User/Desktop/"

extend the folder if necessary:

import os.path
folder = os.path.join( parent_folder, "python stuff" )

add the filename:

file = os.path.join( folder, "Data.txt" )

open the file:

if os.path.exists( file ):
    with open( file, "r"  ) as infile:
        data = infile.read()

or:

import os
import os.path

# get the desktop location ( since this is a special folder )
desktop = os.path.join(( os.environ["userprofile"] ), "desktop" )
file = os.path.join( desktop, "python stuff", "data.txt" )
if os.path.exists( file ):
    with open( file, "r" ) as infile:
        data = infile.read()

Upvotes: 0

Aaron Schapman
Aaron Schapman

Reputation: 1

I had the same problem but now it works for me. I used to open a big map with a lot of sub-maps and the file was in one of the sub-maps. Now I open the submap where my program and file.txt is located and the code open("file.txt", "r") works

Upvotes: 0

Michal M.
Michal M.

Reputation: 91

The problem might be, where exactly you are executing the file.

I had the same problem, but noticed, that the terminal in my IDE (VS Code) is executing the file in a different location. By simply cd to the files location in the terminal, everything went fine. ;)

Upvotes: 1

user7033165
user7033165

Reputation: 97

When using Windows you want to keep in mind that if you name the file data.txt the file will actually be data.txt.txt, however Windows will show it as data.txt because windows hides the file extensions by default. This option can be changed by following the steps in the first comment.

If you then search data.txt there really is no such file.

Upvotes: 7

Roman Belousov
Roman Belousov

Reputation: 161

Please check your current directory.

For that just do this:

import os
print (os.getcwd())

Upvotes: 16

Prashant
Prashant

Reputation: 1064

  • 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 the space in Python stuff is messing things up.

Update: I just tried this out .. seems to be fine with the space actually.

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

Upvotes: 2

Related Questions