Search code examples
pythonoperating-system

How to move to one folder back in python


Actually need to go some path and execute some command and below is the code

code:

import os
present_working_directory = '/home/Desktop/folder' 

presently i am in folder

if some_condition == true :
    change_path = "nodes/hellofolder"
    os.chdir(change_path)
    print os.getcwd()
if another_condition  == true:
    change_another_path = "nodes" 
    os.chdir(change_another_path) 
    print os.getcwd()

**Result**:
'/home/Desktop/folder/nodes/hellofolder'
python: [Errno 1] No such file or directory

Actually whats happening here is when i first used os.chdir() the directory has changed to

'/home/Desktop/folder/nodes/hellofolder',

but for the second one i need to run a file by moving to one folder back that is

'/home/Desktop/folder/nodes'

So can anyone let me how to move one folder back in python


Solution

  • Just like you would in the shell.

    os.chdir("../nodes")