Right way to insert files paths into code

조회 수: 79 (최근 30일)
paul_dak
paul_dak 2021년 7월 15일
댓글: paul_dak 2021년 7월 20일
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
paul_dak
paul_dak 2021년 7월 15일
my idea was to write a function file, that recieves a string of the subfolder name that is located in the project directory, and the function will return its full path.
That way in case of folder changing, i will only have to edit that one function. wonder if there is a better way.
dpb
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
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
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!
paul_dak
paul_dak 2021년 7월 20일
Tnx, looks like what i need :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by