path.exists() is returning False

Question:

I’m building a basic file server and my program cannot find files.

def sendfile(sock, myfile):
    print 'Serving file:', myfile
    print 'File exists?:', os.path.exists(myfile)

    path = os.path.normpath(os.path.join(os.getcwd(), myfile))
    print 'Serving file:', path
    print 'File exists?:', os.path.exists(path)

These always return False even though the ‘myfile’ and ‘path’ are correct [the file is in the same directory as the server program].

IDLE works fine, but without passing to functions.

>>> print os.path.exists("/user/server/foo.txt")  
True

What have I missed?

[EDIT:] Output:

Serving file: foo.txt

File exists?: False
Serving file: /user/server/foo.txt

File exists?: False
Asked By: schroeder

||

Answers:

This may not answer your question directly, but you could go with the “try/except” method:
Whatever function uses the file should return an exception if the file doesn’t exist (especially if it’s a built-in function), and you can act accordingly. Then you have no need to check whether or not the file exists yourself. Dangerous? Perhaps, but that depends on what you are actually trying to do.

I’m almost 100% sure you’re not sanitizing your input before you check if the path exists. Here’s something I ran in my interpreter:

>>> from os.path import exists
>>> exists('dog.png')
True
>>> exists('dog.pngn')
False

Try stripping whitespace on path before you check if it exists.

Answered By: Thane Brimhall

If you read the Python documentation of os.path.exists(), it says that there are specific cases in which a file or folder exists but os.path.exists() returns false:

Return True if path refers to an existing path or an open file
descriptor. Returns False for broken symbolic links. 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.

Answered By: mostafa.elhoushi

As said in this answer
this mainly occurs do to white spaces.

I also faced this issue. It took me a lot of time to figure this out.

python has a function called strip() which removes white spaces.

if variable path_to_file consists the path to the actual file then try using

if path.exists(path_to_file.strip())
        print("file exists")
else:
        print("file doesn't exist")

this worked for me.

Answered By: varun raj

Not directly answering the question stated here, but I found this topic when os.path.exists() was keep giving me “False”, even after using strip() or os.path.join(). In my case, I was using ~ (tylda) to point to the home directory like this:

fileName = "~/path/to/file.txt"

The best way to fix this was to use os.path.expanduser(fileName), and then check if the file exists. Alternatively restore absolute path with os.path.abspath(), followed by removig of “~” from the path (but this solution will not work in all scenarios).

os.path.exists(os.path.abspath(fileName).replace("~",""))

Maybe this will be helpful to someone.

Answered By: Forrest1988

This honestly should be considered a bug if it fails due to spaces.

I have found that in some cases, the result will be spurious, depending somehow on the state of the file server. This does not happen all the time, only once in a while.

I found that with at least a 10 second delay, I could avoid a failure.
In this case, I am opening zip archives repeatedly to access specific zipped files. Prior to the attempt to open it, it checks that the path exists (and try used below this because of this strange issue). If it fails, then it waits in a loop with increasing delay. I found that it usually winds up finding the file exists again after 4 loops (10 sec delay).

Here is the output of my loop print statements:

Archive r:ballotimagearchiveca_san_francisco_2020_prid03.zip does not exist according to os.path.exists().
Waiting 1 seconds
Waiting 2 seconds
Waiting 3 seconds
Waiting 4 seconds
After wait of 10 secs, r:ballotimagearchiveca_san_francisco_2020_prid03.zip now exists according to os.path.exists().

and the code segment that produces this.

    if os.path.isfile(source_path):
    print(f"Verified that {source_path} exists.")
else:
    print(f"Archive {source_path} does not exist according to os.path.exists().")

    # this may be a spurious problem related to using a file server.

    tot_time = 0
    for i in range(1,20):
        print(f"Waiting {i} seconds")
        time.sleep(i)
        tot_time += i 
        if os.path.isfile(source_path):
            print(f"After wait of {tot_time} secs, {source_path} now exists according to os.path.exists().")
            break
    else:
        print(f"After wait of {tot_time} secs, {source_path} still not found according to os.path.exists().")
        sys.exit(1)
Answered By: Ray Lutz

I had this same issue and found the following solution:

Using the OS’s native directory separator ” or ‘/’ even on Windows, using the forward slash in all occasions works just fine for me. os.sep can help.

In addition to this, sanitizing the path string a bit similar to this:

import re
from os import path
strPath = "c:/dir1/dir2/dir3/dir4/important-file.ext"
strPath = re.escape(strPath)

bTest = os.access(strPath, os.F_OK)

# or the classic
bTest = path.exists(strPath)
print(bTest)


Answered By: Leif

I want to use the file in the Downloads folder on ubuntu, but os.path.exists can’t find using the absolute path ~/Downloads/filename.txt.

Then I use os.path.abspath('') to get the root path /home/yourPCname/ and replace ~, it works!

Answered By: bluebluesky

os.path.exists returns false also if the given path is longer than 256 characters.

Answered By: asiajo

I was getting this problem fairly often until I tried adding getcwd(), now it never fails

os.path.join(os.getcwd(), source_file)
Answered By: QuentinJS
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.