18

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' 
3
  • 1
    As far as I can see your code is correct. The whitespace shouldn't even be a problem. Commented Jan 8, 2015 at 21:56
  • You don't have to specify the "r" flag in the open function, since it's the default one. Commented Jan 8, 2015 at 21:56
  • I'm almost afraid to ask, but is the file actually there? Commented Jan 8, 2015 at 23:16

10 Answers 10

16

Please check your current directory.

For that just do this:

import os print (os.getcwd()) 
Sign up to request clarification or add additional context in comments.

1 Comment

A file in the current working directory is not found: so there's something more required here.e
10

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.

1 Comment

Using VSCode right now, just burned up more than an hour with this issue, thank for the fix! (You can also open up the integrated console in VS code and cd to the applicable directory. Either way works.)
7

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.

2 Comments

"windows hides the file extentions" This isn't necessarily true. One can reveal file extensions by going to File Explorer options > View and unchecking Hide extensions for known file types.
" if you name the file data.txt the file will actually be data.txt.txt" - this is not necessarily true. It depends on what tool you're using to name the file.
2
  • 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'] 

3 Comments

Thanks for the help so far, the py file and txt file are currently in the same directory.
Thanks Prashant, the problem seems to have resolved itself and I'm not sure why. I started working my way through the examples you suggested and ended up accidentally rewriting the f = open line as a write, instead of read, which wrote a second file next to the first. I then deleted the new file, changed the write to a read and subsequently python could locate the file. I'm still not sure why, regardless thanks for all your help!
1

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. ;)

Comments

0

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

Comments

0

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() 

3 Comments

Seems very complicated. What is the advantage over simple open?
It uses the open function, advantages? with open instead of open removes the necessity to remember to close the opened file. also, using the folder building process, you never have to remember the path to your folder, AND, it is usable on a different machine, not only yours.
I get that. But OP says the file is in the same folder, why not use relative import? Just import os; filename = "Data.txt"; if os.path.exists(filename): with open(filename, "r") as file: data = file.read()
0

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...

Comments

0

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

Comments

-2

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 :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.