python os.path.exists reports False when files is there

2024/4/15 2:14:16

Hi have an application which is sometimes reporting that a file does not exist even when it does, I am using os.path.exists and the file is on a mounted network share. I am on OSX Yosemite, python 2.7.9 and I have access rights to the file. Here's the weird thing. The first command below reports False, I then run it again but change one of the characters in the file name to lowercase (TestVendorid_VerifyLog.txt), run it again and it reports True! Then run it again with the upper case again (TestVendorId_VerifyLog.txt) and it reports True! What is going on? This is quite consistent, in that it returns True most of the time, but then all of a sudden return False, then I can repeat the below exercise.

>>> import os
>>> os.path.exists("/Volumes/platform-deliveries-103_1/TEST/TestVendorId.itmsp/TestVendorId_VerifyLog.txt")
>>> os.path.exists("/Volumes/platform-deliveries-103_1/TEST/TestVendorId.itmsp/TestVendorid_VerifyLog.txt")
>>> os.path.exists("/Volumes/platform-deliveries-103_1/TEST/TestVendorId.itmsp/TestVendorId_VerifyLog.txt")


When it is reporting True, I ran this:

>>> os.stat("/Volumes/platform-deliveries-103_1/TEST/TestVendorId.itmsp/TestVendorId_VerifyLog.txt")
posix.stat_result(st_mode=33216, st_ino=5351561660274954203, st_dev=771751953L, st_nlink=1, st_uid=504, st_gid=20, st_size=38552, st_atime=1428492003, st_mtime=1428589374, st_ctime=1428589374)


OK, I can now repeat this and it is definitely a cache thing. I delete the file TestVendorId_VerifyLog.txt on my local Mac, then recreate the file on another workstation (this file is on a network share) I then get False on my Mac. If I change a letter in the file name of the os.path.exists command, it seems to make os.path.exists take a harder look for the file and finds it. So what I need is a 'Refresh Finder' command in python just prior to running the command.


os.path.exists returns False when an OSError is happening while it tries to stat the file. The exception is handled and therefor masked.

You should try to run os.stat(filename) to see if that gives further information on the problem.

Related Q&A

Python unhashable type: numpy.ndarray

I worked on making functions for K Nearest Neighbors. I have tested each function separately and they all work well. However whenever I put them together and run KNN_method, it shows unhashable type: n…

Efficient way to generate Lime explanations for full dataset

Am working on a binary classification problem with 1000 rows and 15 features. Currently am using Lime to explain the predictions of each instance. I use the below code to generate explanations for full…

how to handle javascript alerts in selenium using python

So I there is this button I want to click and if its the first time youve clicked it. A javascript alert popup will appear. Ive been using firebug and just cant find where that javascript is located an…

testing.postgresql command not found: initdb inside docker

Hi im trying to make a unittest with postgresql database that use sqlalchemy and alembicAlso im running it on docker postgresqlIm following the docs of testing.postgresql(docs) to set up a temporary po…

Recommended approach for loading CouchDB design documents in Python?

Im very new to couch, but Im trying to use it on a new Python project, and Id like to use python to write the design documents (views), also. Ive already configured Couch to use the couchpy view server…

Error when import matplotlib.pyplot as plt

I did not have any problem to use "plt", but it suddenly shows an error message and does not work, when I import it. Please see the below. >>> import matplotlib >>> import m…

Python NtQueryDirectoryFile (File information structure)

Ive written a simple (test) script to list files in a selected directory. Not using FindFirstFile; only native API. When I execute the script and watch, Win32API monitor tells me STATUS_SUCCESS. My Fil…

returning A DNS record in dnspython

I am using dnspython to get the A record and return the result (IP address for a given domain).I have this simple testing python script:import dns.resolverdef resolveDNS():domain = "…

IO completion port key confusion

Im writing an IO completion port based server (source code here) using the Windows DLL API in Python using the ctypes module. But this is a pretty direct usage of the API and this question is directed…

PySpark reversing StringIndexer in nested array

Im using PySpark to do collaborative filtering using ALS. My original user and item ids are strings, so I used StringIndexer to convert them to numeric indices (PySparks ALS model obliges us to do so).…