1

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

3
  • 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 Commented Mar 16, 2021 at 23:02
  • 2
    If you want a file dialog, see the answers to this question: Quick and easy file dialog in Python? Commented Mar 16, 2021 at 23:05
  • Checkout Python's pathlib library: docs.python.org/3/library/pathlib.html for handling paths and working with files. Commented Mar 16, 2021 at 23:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.