Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 221
Shiva Krishna Bavandla Avatar asked Sep 05 '12 11:09

Shiva Krishna Bavandla


People also ask

How do I navigate to a folder in Python shell?

The Python Command PromptUse "cd" to change your directory to the folder with the current version of Python you want to use (i.e. C:/Python26/ArcGIS10. 0). Type "dir" in this folder and you'll see "python.exe".


2 Answers

Just like you would in the shell.

os.chdir("../nodes") 
like image 145
Alex Avatar answered Sep 28 '22 07:09

Alex


Here is a very platform independent way to do it.

In [1]: os.getcwd() Out[1]: '/Users/user/Dropbox/temp'  In [2]: os.path.normpath(os.getcwd() + os.sep + os.pardir) Out[2]: '/Users/user/Dropbox/' 

Then you have the path, and you can chdir or whatever with it.

like image 35
chimpsarehungry Avatar answered Sep 28 '22 07:09

chimpsarehungry