EXIST ignores leading slash
Afficher commentaires plus anciens
The EXIST function is reporting that a file exists when it does not. I'm trying to test the existence of the file '/license/license.json', which does not exist on my file system. The file 'license.json' is in my path, but the directory '/license' does not exist on my system.
>> exist('/license', 'dir')
ans =
0
>> exist('/license/license.json', 'file')
ans =
2
>> which('/license/license.json')
/Users/brian/apps/copilot/license/license.json
>> which('license.json')
/Users/brian/apps/copilot/license/license.json
Am I misunderstanding the use of EXIST? Why is it ignoring the leading slash in an absolute path?
UPDATE: a workaround to check the existence of a file given its absolute path is provided by OCDER below:
FileLoc = dir('/license/license.json')
IsFileThere = ~isempty(FileLoc) && any(~[FileLoc.isdir]);
8 commentaires
Walter Roberson
le 25 Juil 2018
The behaviour of which when given a path is not really specified. The documentation says
If item is a file name including the extension, and it is in the current working folder or on the MATLAB path, then which displays the full path of item.
which is not incompatible with the idea that the leading part would be stripped off and then the working folder and MATLAB path checked for what was left.
Walter Roberson
le 25 Juil 2018
In a quick test on my OS-X system, MATLAB appears to be respecting leading /, at least in the version I tested.
Which release are you using?
Brian Keating
le 25 Juil 2018
Brian Keating
le 25 Juil 2018
OCDER
le 25 Juil 2018
Did this work?
exist('/Users/brian/apps/copilot/license', 'dir')
Brian Keating
le 25 Juil 2018
So what's the unexpected result then? Use this to determine what a leading slash is considered by exist.
SlashDir = dir('/*');
SlashDir(1).folder
%I get 'C:\' for windows 10
Walter Roberson
le 26 Juil 2018
Okay, I am able to reproduce this on OS-X in the circumstance that the directory containing the license directory is on the path (regardless of my current directory.)
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!