List files with the use of dir

Hi. I need to convert the following matlab code to python:

DirList = dir(fullfile(Folder, '*.mat'));
Data = cell(1, length(DirList));

Is there a simple way to do it in python?

This question is related to general python and not related to pytorch as such.

As I understand, the code is trying to get the list of .mat files in the given path.
You may want to look at pathlib or os.path libraries to work with & manipulate folder paths.

Below is one of the ways to achieve what you want.

from pathlib import Path
folder = './rootpath' # give the root path here
folderpath = Path(folder)
dirlist = [str(p.absolute()) for p in list(folderpath.glob('*.mat'))]

Thank you very much. Just an other question: in matlab dirlist is a struct with 6 fields (name, folder, date,…) and I could get its attributes with:


How can I do the same in python since dirlist is now a list of strings?

Basically, list(folderpath.glob('*.mat')) gives a set of Path objects corresponding to .mat files in that directory.
You can call the object’s methods and attributes to get the info you want.

You can refer to the available methods on this object here: