13

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'
Cœur
  • 37,241
  • 25
  • 195
  • 267
user4434674
  • 193
  • 1
  • 1
  • 8

10 Answers10

11

Please check your current directory.

For that just do this:

import os
print (os.getcwd())
anothernode
  • 5,100
  • 13
  • 43
  • 62
7

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.

Fletchius
  • 438
  • 4
  • 13
  • 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.) – tonethar Jul 12 '23 at 15:39
5

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.

The BrownBatman
  • 2,593
  • 1
  • 13
  • 29
user7033165
  • 77
  • 1
  • 2
  • 7
    "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**. – The SE I loved is dead Oct 17 '16 at 23:55
  • 1
    _" 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. – Bryan Oakley Mar 17 '20 at 15:55
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']
Prashant
  • 1,014
  • 11
  • 28
  • Thanks for the help so far, the py file and txt file are currently in the same directory. – user4434674 Jan 08 '15 at 22:03
  • Can you give https://mail.python.org/pipermail/tutor/2008-August/063922.html a try? – Prashant Jan 08 '15 at 22:10
  • 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! – user4434674 Jan 10 '15 at 13:17
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. ;)

Michal M.
  • 71
  • 3
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

mohammed wazeem
  • 1,310
  • 1
  • 10
  • 26
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()
structure
  • 35
  • 7
  • Seems very complicated. What is the advantage over simple `open`? – Brambor Dec 23 '20 at 15:31
  • 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. – structure Dec 27 '20 at 03:44
  • 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()` – Brambor Dec 28 '20 at 08:57
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...

ryan muir
  • 21
  • 5
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

0rfanidis
  • 1
  • 1
-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 :)

wohlstad
  • 12,661
  • 10
  • 26
  • 39