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
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".
Just like you would in the shell.
os.chdir("../nodes")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With