0

I would like to know how can I open and navigate through folders in the OS in Python the same way I can do it in MATLAB. I have tried using some os functions but none of them seems to make a lot of sense for what I need.

Basically, I need a code where I open a folder on Windows, get the directory, search for the filename I want and read the data inside. I have a MATLAB code that does this exact process below:

P = fileparts(mfilename('fullpath')); %Opening a folder

nfolder = uigetdir(P,'Select the file directory'); %Opens a File Open/Save window to find the folder where the sequence is 
if~ischar(nfolder)
    disp('Invalid Directory')
    return;
end

prefix = nfolder; %Directory
comp = '/name-of-the-file';
ext = '.extension';

fname = [prefix comp ext]; %Path

delimiterIn=' ';
headerlinesIn=11;
Db=importdata(fname,delimiterIn,headerlinesIn); %Import the data (basically numbers)
Db=Db.data;

I would really appreciate any help

  • well if you have the prefix of the path and the suffix, you can use ```path = os.path.join()```. If there are other path elements between your prefix and suffix just include them in their relevant positions – Kaleba KB Keitshokile Mar 16 '21 at 23:02
  • 2
    If you want a file dialog, see the answers to this question: [Quick and easy file dialog in Python?](https://stackoverflow.com/questions/9319317/quick-and-easy-file-dialog-in-python/) – luuk Mar 16 '21 at 23:05
  • Checkout Python's `pathlib` library: https://docs.python.org/3/library/pathlib.html for handling paths and working with files. – Azim J Mar 16 '21 at 23:13

0 Answers0