Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the parent directory of the current working directory in Julia

This seems to be a simple question but I can't find any answer online. Suppose my current working directory is C:/parent_folder/sub_folder, and I want to get C:/parent_folder in Julia.

Edit: I have a solution using PyCall but are there any better solutions, preferably without the need of importing libraries of other languages?

using PyCall 

function get_parent_directory()

    pathlib = pyimport("pathlib")
    path = pathlib.Path(pwd())
    s = string(path.parent)
    return split(s, "\'")[2]

end

get_parent_directory()
like image 203
Sato Avatar asked Dec 24 '19 18:12

Sato


People also ask

How do I get the parent directory of a file?

Use File 's getParentFile() method and String. lastIndexOf() to retrieve just the immediate parent directory.

How do I get the current working directory?

Getwd functionis used to get the current working directory in Golang, the function returns the rooted path name and if the current directory can be reached via multiple paths, the function can return any one of them.


2 Answers

An alternative solution is to use dirname:

dirname(pwd())
like image 51
David Varela Avatar answered Sep 18 '22 01:09

David Varela


This should work for you:

cd(pwd, "..")
like image 44
Bogumił Kamiński Avatar answered Sep 21 '22 01:09

Bogumił Kamiński