-2

Want to move up one directory from a given directory. I achieve this by doing:

import os
os.chdir(given_dir)
os.chdir('..')

But, I was wondering if there was a better more explicit way using (ideally) one statement or if there exists a built-in feature that I may not be aware of.

Yannis
  • 1,682
  • 7
  • 27
  • 45
  • 3
    `os.chdir(os.path.dirname(given_dir))`? – Selcuk Apr 06 '16 at 12:29
  • 3
    http://stackoverflow.com/questions/2860153/how-do-i-get-the-parent-directory-in-python – Paulo Almeida Apr 06 '16 at 12:31
  • &Paulo Almeida: I have seen that post, thanks. But it does not apply exactly to what I am asking which is that from a given dir to move up. Could you explain the downvote please? – Yannis Apr 06 '16 at 12:36
  • 2
    @Selcuk: That's it! Thanks! – Yannis Apr 06 '16 at 12:47
  • @Yannis I wasn't the one who downvoted (easy to verify, since I have 0 downvotes). That said, the question I linked to gives you the answer. Once you have the parent, you just have to `chdir` to it. According to the answers I linked to, `os.path.dirname` can give you the wrong result depending on trailing slash. – Paulo Almeida Apr 06 '16 at 12:52
  • 1
    What do you mean when you say its not pythonic??? It is standard parent directory relative path. Can you explain how it has anything to do with python idioms? – C Panda Apr 06 '16 at 12:52
  • @PauloAlmeida From a post similar to the one you linked I 've seen how to do this using 2 statements. So, thank you for that. I was wondering whether or not there was a more explicit way using perhaps a method I was not aware of. – Yannis Apr 06 '16 at 12:56
  • @CPanda By 'not Pythonic' I mean to write something using 2 statements when you could (perhaps) do it using 1 more explicitly. Thanks for the downvote. – Yannis Apr 06 '16 at 12:58
  • 1
    @Yannis With [unipath](https://github.com/mikeorr/Unipath) you could do it like `os.chdir(path.parent)`. – Paulo Almeida Apr 06 '16 at 13:06
  • 1
    @Yannis Have you typed `os.pardir` in REPL yet? What does it show? It shows `..`. Did you see the output of `os.path.join(given_dir, os.pardir)`. You can see `..` in that. Using absolute paths as opposed to relative ones are more of a program requirement than an idiom of any particular language. – C Panda Apr 06 '16 at 13:08
  • @PauloAlmeida unipath seems really interesting, thanks – Yannis Apr 06 '16 at 13:44
  • @CPanda Appreciate the detailed explanation, thanks. A complete answer answer should ideally include this info as well regarding for the 'best approach' concerns. – Yannis Apr 06 '16 at 13:46

1 Answers1

2

How about

import os, os.path
print os.chdir(os.path.join(given_dir, os.pardir))

OR

os.chdir(os.path.dirname(given_dir))

(as Selcuk suggested)

Yannis
  • 1,682
  • 7
  • 27
  • 45
  • 2
    It works great, thanks. The 'os,pardir' is very nice suggestion, did not know about. And, just for having a more complete answer, I edited yours to include Selcuk's comment which also works; although not sure which is one is better.. – Yannis Apr 06 '16 at 12:50