1
$\begingroup$

Consider some directory

.../a/

where ... may be some arbitrary path.

It has two subdirectories:

/a/b/

and

/a/c/

In the subdirectory /a/c/, a Mathematica notebook is located, while in the subdirectory /a/b/, a file file.txt is located.

How to import the file.txt in the Mathematica notebook? Precisely, what is the Mathematica's analog of .. used in Linux in order to go from the NotebookDirectory[] to the upper directory?

$\endgroup$
2
  • 4
    $\begingroup$ I believe you are looking for: ParentDirectory[NotebookDirectory[]] $\endgroup$
    – Syed
    Aug 12, 2022 at 11:30
  • 2
    $\begingroup$ "." and ".." work in MMA the same as in Unix. E.g. FileNames[All, "."] gives all names in the current working directory. And "FileNames[All, ".."]" all names in its parent directory. $\endgroup$ Aug 12, 2022 at 11:45

1 Answer 1

1
$\begingroup$

Notebook's Directory:

acDir = NotebookDirectory[]
(* "C:\\Users\\rpotter\\Desktop\\" *)   

Directory above that:

aDir = ParentDirectory[acDir]
(* "C:\\Users\\rpotter" *)

"b" directory in directory above notebook's directory:

abDir = FileNameJoin[{aDir, "bDir"}]
(* "C:\\Users\\rpotter\\bDir" *)

Full path of file:

myFilePath = FileNameJoin[{abDir, "file.txt"}]
(* "C:\\Users\\rpotter\\bDir\\file.txt" *)

Or all at once with:

myFilePath = 
 FileNameJoin[{ParentDirectory[NotebookDirectory[]], "bDir", 
   "file.txt"}]
(* "C:\\Users\\rpotter\\bDir\\file.txt" *)

Then import with:

fileData=Import[myFilePath]
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.