MATLAB实现:
function scanDir(root_dir)
% if isempty(varargin)
% root_dir = uigetdir;
% end
% files={};
if root_dir(end)~='\'
root_dir=[root_dir,'\'];
end
fileList=dir(root_dir); %扩展名
n=length(fileList);
for i=1:n
if strcmp(fileList(i).name,'.')==1||strcmp(fileList(i).name,'..')==1
continue;
else
fileList(i).name
if ~fileList(i).isdir
full_name=[root_dir,fileList(i).name];
else
scanDir([root_dir,fileList(i).name]);
end
end
end
end
Python实现:
# folder_name:最外层文件夹的路径
for root, dirs, files in os.walk(folder_name):
files = sorted(files)
for name in files:
print(os.path.join(root,name))