Moving up one directory in Python

47

Is there a simple way to move up one directory in python using a single line of code? Something similar to cd .. in command line

This question is tagged with python directory

~ Asked on 2013-07-26 15:37:37

The Best Answer is


83

>>> import os
>>> print os.path.abspath(os.curdir)
C:\Python27
>>> os.chdir("..")
>>> print os.path.abspath(os.curdir)
C:\

~ Answered on 2013-07-26 15:39:55


38

Use the os module:

import os
os.chdir('..')

should work

~ Answered on 2013-07-26 15:39:30


Most Viewed Questions: