Python change current directory(up to parent) 3 Examples

Getting the current folder and moving one or several levels up is possible in Python 3 with several different options:

  • os.chdir("..")
  • pathlib - p.parent
  • os.chdir(os.path.dirname(os.getcwd()))

In this post:

Moving one directory up with os.chdir("..")

The most popular way and the one compatible with older python versions is os.chdir(".."). Below you can find the usage of it:

import os
print(os.path.abspath(os.curdir))
os.chdir("..")
print(os.path.abspath(os.curdir))

result:

/home/user/PycharmProjects/python/test/Files
/home/user/PycharmProjects/python/test

You can move several levels up with this syntax:

import os
print(os.path.abspath(os.curdir))
os.chdir("../../..")
print(os.path.abspath(os.curdir))

result:

/home/user

Moving one directory up with pathlib - p.parent

Another way of working with folders and files was introduced since Python 3.4 - pathlib. It provides methods and information related to files and folders:

  • get parent folder(or parent of the parent)
  • get file name and absolute path
  • get statistics for the file
  • check if the object is a file or a directory
from pathlib import Path
p = Path("/home/user/myfile.txt")
print(p.parent)
print(p.parent.parent)

print(p.name)
print(p.as_posix())
print(p.stat())

print(p.is_dir())
print(p.is_file())

result:

/home/user/
/home
myfile.txt
/home/user/myfile.txt
os.stat_result(st_mode=33204, st_ino=16515537, st_dev=64769, st_nlink=1, st_uid=1000, st_gid=1000, st_size=79152, st_atime=1536731949, st_mtime=1536731949, st_ctime=1536731949)
False
True

Moving up with os.chdir(os.path.dirname(os.getcwd()))

Another way is possible from the module os by using: os.chdir(os.path.dirname(os.getcwd())). Below you can see the example how to change the working folder from the python script itself:

import os
print(os.path.dirname(os.getcwd()))
os.chdir(os.path.dirname(os.getcwd()))
print(os.path.dirname(os.getcwd()))

result:

/home/user/PycharmProjects/python/test/Files
/home/user/PycharmProjects/python/test

You can move several levels up with this syntax:

import os
print(os.path.abspath(os.curdir))
os.chdir("../../..")
print(os.path.abspath(os.curdir))

result:

/home/user/PycharmProjects/python/test/Files
/home/user/PycharmProjects/python/test

Note that once the directory is changed the result with one of this methods than the current folder will be different for the script. In other words:

This code will produce output which is moving up to the root folder:

import os
print(os.path.dirname(os.getcwd()))
os.chdir(os.path.dirname(os.getcwd()))
print(os.path.dirname(os.getcwd()))
os.chdir(os.path.dirname(os.getcwd()))
print(os.path.dirname(os.getcwd()))
os.chdir(os.path.dirname(os.getcwd()))
print(os.path.dirname(os.getcwd()))

result:

/home/user/PycharmProjects/python/test
/home/user/PycharmProjects/python
/home/user/PycharmProjects
/home/user

Python move back one folder

If you want to move back one folder then you can try with:

import os
print(os.path.abspath(os.curdir))
print(os.path.normpath(os.getcwd() + os.sep + os.pardir))
print(os.path.abspath(os.curdir))

result:

/home/user/PycharmProjects
/home/user/PycharmProjects/python

or by

import os
print(os.path.abspath(os.curdir))
os.chdir("../python")
print(os.path.abspath(os.curdir))

result:

/home/user/PycharmProjects
/home/user/PycharmProjects/python