6

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.

speedyrazor
  • 3,127
  • 7
  • 33
  • 51

2 Answers2

3

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.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
0

Make sure there is not a \n or any other 'invisible' character anywhere in your filename.

Edit: After a few hours digging on a similar problem of my own, I realized the problem was wrong permission for the directory where my target file was in. Make sure you ls -l on your directory to check whether the user you are using has the proper permission.

joarleymoraes
  • 1,891
  • 14
  • 16