Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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")
False
>>> os.path.exists("/Volumes/platform-deliveries-103_1/TEST/TestVendorId.itmsp/TestVendorid_VerifyLog.txt")
True
>>> os.path.exists("/Volumes/platform-deliveries-103_1/TEST/TestVendorId.itmsp/TestVendorId_VerifyLog.txt")
True
>>> 

UPDATE 1:

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)

UPDATE 2:

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.

like image 871
speedyrazor Avatar asked Apr 09 '15 14:04

speedyrazor


1 Answers

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.

like image 129
Klaus D. Avatar answered Nov 14 '22 09:11

Klaus D.