schroeder
schroeder

Reputation: 559

path.exists() is returning False

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

Upvotes: 28

Views: 76029

Answers (10)

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. It's actually more dangerous to rely on path.exists when you run the risk of race conditions. Instead, handle the real error that arises from not finding the file, since you usually need to handle that anyways (imagine the file gets created/deleted between your check and the code that uses it).

I do recognize there are times when you need to check if a file exists for performance reasons or for lock-files, but most of the time you should focus on why you need the file to exist.

Upvotes: 3

QuentinJS
QuentinJS

Reputation: 322

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

os.path.join(os.getcwd(), source_file)

Upvotes: 1

asiajo
asiajo

Reputation: 31

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

Upvotes: 2

bluebluesky
bluebluesky

Reputation: 5

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!

Upvotes: 0

Leif
Leif

Reputation: 1

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)


Upvotes: 0

Ray Lutz
Ray Lutz

Reputation: 79

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:\ballotimagearchive\ca_san_francisco_2020_pri\d03.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:\ballotimagearchive\ca_san_francisco_2020_pri\d03.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)

Upvotes: 1

Forrest1988
Forrest1988

Reputation: 121

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.

Upvotes: 12

varun raj
varun raj

Reputation: 31

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.

Upvotes: 3

mostafa.elhoushi
mostafa.elhoushi

Reputation: 5312

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.

Upvotes: 19

Thane Brimhall
Thane Brimhall

Reputation: 9555

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.png\n')
False

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

Upvotes: 28

Related Questions