How to get the parent directory of the current working directory in Julia
Asked Answered
B

3

7

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()
Betrothal answered 24/12, 2019 at 18:18 Comment(0)
C
4

This should work for you:

cd(pwd, "..")
Chair answered 24/12, 2019 at 19:13 Comment(0)
J
11

An alternative solution is to use dirname:

dirname(pwd())
Joub answered 24/12, 2019 at 21:43 Comment(0)
C
4

This should work for you:

cd(pwd, "..")
Chair answered 24/12, 2019 at 19:13 Comment(0)
N
2

If you want to get the parent directory (without changing the working directory), you can do the following:

splitdir(pwd())[1]
Naquin answered 2/8, 2022 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.