Right way to insert files paths into code
조회 수: 79 (최근 30일)
이전 댓글 표시
Hi there!
Im working in the data science world, and wonder what is the best way to insert a file path into a code.
Since i have many scripts and classes, and each of them contain folder/files paths within the code (i.e. reading data from server - G:/My_folder/.../my_file), im finiding it problematic when i want to change folder names or to reorginize my directories.
Do you have any wise suggestions how to keep my code rebust enought so this directories changes wont impact it in future.
Tnx!
댓글 수: 7
dpb
2021년 7월 15일
I personally detest building such complex trees but recognize there may be reasons for such deep nesting--but in 40 years consulting I've never come across a case I thought I needed or was well served by more than a couple deep.
That aside, this again is a case of "why is the folder name going to change?" and what's going to determine it needs to change and how is that determined and what sets the new name?
If these data are coming from an external source, then they control the start, not you and you need a database structure to handle that if more than one.
Then, looking at the sample above, presuming that you are creating this nested structure from results, if you maintain the same structure under the top level as I would strongly suggest, then you can use relative addressing from the root directory and never change anything except the root and not even have to code the absolute names at all.
Or, if you have a naming scheme for the subdirectories, similarly, build those dynamically and again only keep the root name.
채택된 답변
Rik
2021년 7월 15일
편집: Rik
2021년 7월 15일
Personally I use a function like the one below to semi-hard-code the paths. The context for this is that I have a synced folder, but on one system it is 'C:\User\#me#\#sync software#\project', while on another it is 'B:\#sync software#\project'.
If you have any way to automatically determine the path of the network drive, you can use a function like this to retrieve all sub-directory paths.
function p=places(id)
%return value has no trailing filesep
root_project_folder=fileparts(fileparts(mfilename('fullpath')));
switch id
case 0
p=root_project_folder;
case 1
p=fullfile(root_project_folder,'scripting');
case 2
p=fullfile(root_project_folder,'image_output');
case 3
p=fullfile(root_project_folder,'SPSS_output');
otherwise
error('unknown id')
end
end
This way, I can move the project folder around without having to rename anything, and if I need a static path, I have only 1 place need to edit.
댓글 수: 2
Stephen23
2021년 7월 15일
편집: Stephen23
2021년 7월 16일
+1 regardless of the particular approach, I think the key is described in this answer: the path/s should only be defined in one location. Exactly how it works is a matter of taste: all of the functions/classes could pass the path as input/output argument, read some config file, or use something like what Rik shows above. Relative filenames might be very useful in this situation.
In any case, definitely avoid hard-coding the path into every script, function, or class!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Search Path에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!