EXIST ignores leading slash
古いコメントを表示
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 件のコメント
Walter Roberson
2018 年 7 月 25 日
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
2018 年 7 月 25 日
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
2018 年 7 月 25 日
Brian Keating
2018 年 7 月 25 日
OCDER
2018 年 7 月 25 日
Did this work?
exist('/Users/brian/apps/copilot/license', 'dir')
Brian Keating
2018 年 7 月 25 日
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
2018 年 7 月 26 日
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.)
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!