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에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!