2

I wrote the following code to open a specific file. The file definitely exists, so why does Python say no such file?

try:
    fh = open("F:/EveryThing! Python/CorePython/Strings/tester.txt");
    strg = fh.read();
    print (strg);
except IOError, e:
    print e;
    print "outputting e",e.args;
finally:
    print "This is bound to be executed";

This outputs:

[Errno 2] No such file or directory: 'F:/EveryThing! Python/CorePython/Strings/tester.txt'
outputting e (2, 'No such file or directory')
This is bound to be executed
davidism
  • 98,508
  • 22
  • 317
  • 288
Farhan Shirgill Ansari
  • 13,172
  • 7
  • 49
  • 95
  • 5
    Style tip: You do not need to end lines in `;` like you do in C/C++. Doing so in Python is simply redundant. –  Nov 16 '14 at 17:17
  • 2
    If you are using `F:` I guess it's Windows. On Windows you have `\ ` not `/`. Also in Python you will have to quote them as `\\ `. – Klaus D. Nov 16 '14 at 17:18
  • another style tip: with open(...) as f: d = f.read() - closes the file in any case and easier than try/finally. – Thomas Waldmann Nov 16 '14 at 17:20
  • 3
    @KlausD. - Python is smart enough to work with either `\ ` or `/` on Windows. In fact, using `/` is encouraged by many programmers to avoid having to escape things. –  Nov 16 '14 at 17:21
  • 2
    @KlausD., why would forward slashes not work in windows? – Padraic Cunningham Nov 16 '14 at 17:23

2 Answers2

0

You must be designating the filename incorrectly, and thus according to python it does not "definitely exist". Use os.path.exists to check if the file does, indeed, exist at the given location. For example, I've made a file "a.txt" in the directory where the code below is run, but "b.txt" does not exist:

import os

print os.path.exists("a.txt")
print os.path.exists("b.txt")

try:
    open("a.txt")
except IOError, e:
    print e

try:
    open("b.txt")
except IOError, e:
    print e

Here the output is:

> python ff.py 
True
False
[Errno 2] No such file or directory: 'b.txt'
Hooked
  • 70,732
  • 35
  • 167
  • 242
  • I am not designating the filename incorrectly. Doing it with BufferedReader(new FileReader()) in java, I am able to read it in java.. – Farhan Shirgill Ansari Nov 16 '14 at 17:48
  • going through the link... On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists. How would I know that permission has been granted to execute os.stat() on the requested file. – Farhan Shirgill Ansari Nov 16 '14 at 17:51
  • 1
    @ShirgillAnsari It is possible that the file has strange permissions that your python interpreter doesn't have (though unlikely). Try copying the file to the same directory as the python script and renaming it to something simple. See if that works. Then, use the original filename but in the local path. See if that works. Hopefully somewhere down the way you'll find the issue. – Hooked Nov 16 '14 at 18:13
  • Hooked's suggestion got me on the right track. Once I copied the file and gave it a different name, I was able to read it back out. But as soon as I started using only the new name (discarding the old one) the problem came back. Which narrowed down the problem, and led me to find out what I was doing wrong -- before reading the file back out, I was not closing it first. So thank you Hooked. Here's where I found the answer https://stackoverflow.com/a/56514306/9645190 – geekandglitter Nov 22 '20 at 11:40
-1

I just encountered the same error and I suspect the reason for yours is the same as for mine. If you're using Windows 10, make sure that you're accessing the directory through OneDrive instead of following the path to the desktop immediately from the user. So instead of typing

open("C:/Users/UserName/Desktop/FileName.ext");

write

open("C:/Users/UserName/OneDrive/Desktop/FileName.ext");

You can check what files are in the current directory by typing

import os;
os.chdir("directoryPath");
os.listdir();

Source: https://stackoverflow.com/a/12202004/8955961

Alex S
  • 125
  • 6
  • 1
    Why would OneDrive even come into picture here? – Kitwradr May 29 '20 at 08:20
  • @Kitwradr Because that was what corrected the issue in my case. It may be useful for others who use Win10. – Alex S May 30 '20 at 11:17
  • That is a case of wrong path to the file which is blatantly obvious and not the question here – Kitwradr Jun 01 '20 at 08:20
  • 1
    @Kitwradr Okay... I understand what you're saying, but what I don't understand is why it's an issue. The purpose of my post was to be helpful even if that meant I was not completely certain whether it would resolve the poster's question. – Alex S Jun 02 '20 at 06:52