How to move to one folder back in python
Asked Answered
C

9

48

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

Centre answered 5/9, 2012 at 11:10 Comment(2)
Avoid os.chdir if you can. The subprocess module's functions take the working directory as an argument. (Also, true should be True and == True is never necessary.)Exogenous
@Kour ipm, as larsmans said, do what you need to do using subprocess, it has the keyword cwd. So call what you need using: subprocess.call("yourCommand", shell=True, cwd="path/to/directory")Limber
K
65

Just like you would in the shell.

os.chdir("../nodes")
Ketty answered 5/9, 2012 at 11:13 Comment(0)
T
54

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.

Tire answered 18/7, 2013 at 14:51 Comment(2)
what about ''/Users/user/ how can i get it?Theravada
thank you for adding this.it should be the actual answerGallice
S
35

Just call

os.chdir('..')

the same as in any other language :)

Salomie answered 5/9, 2012 at 11:13 Comment(1)
Tried using this and it does not work. Once I try changing using this my path becomes NoneCentimeter
J
6

Exact answer for your question is os.chdir('../')

Use case:

Folder1:
    sub-folder1:(you want to navigate here)
Folder2:
    sub-folde2:(you are here)

To navigate to sub-folder1 from sub-folder2, you need to write like this "../Folder1/sub-folder1/"

then, put it in os.chdir("../Folder1/sub-folder1/").

Juggins answered 11/5, 2019 at 18:30 Comment(0)
K
2

think about using absolute paths

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

if some_condition == true :
    path = os.path.join(pwd, "nodes/hellofolder")
    os.chdir(path)
    print os.getcwd()
if another_condition  == true:
    path = os.path.join(pwd, "nodes")
    os.chdir(path) 
    print os.getcwd()
Kamat answered 5/9, 2012 at 11:20 Comment(0)
B
1

My problem was fixed with this command first import os and after add os.path.normpath(os.path.abspath(__file__) + os.sep + os.pardir)

Burleigh answered 7/11, 2019 at 17:46 Comment(0)
C
1

The answers mentioned above are correct. The following is more of something that usually happens when your Python script is in a nested directory. You want to go one level up from the current working directory to maybe, let's say, load a file.

The idea is to simply reformat the path string and prefix it with a '../'. So an example would be.

'../current_directory/' + filename

This format is similar to when used in a terminal. Whenever in doubt fire up a terminal and experiment with some commands. The format is reflected in the programming language.

Clockwork answered 11/5, 2020 at 7:27 Comment(0)
F
0

Define this function in your script and call it whenever you want to go back just by one folder:

import os

def dirback():
    m = os.getcwd()
    n = m.rfind("\\")
    d = m[0: n+1]
    os.chdir(d)
    return None
Forsythia answered 11/2, 2022 at 12:54 Comment(0)
V
0
import os
pathX = r'C:/Hello/HelloWorld'
print(os.path.dirname(pathX))
Vallation answered 25/1 at 12:27 Comment(1)
Please explain the effect of what you propose and how it helps to solve the problem. Try for How to Answer and consider taking the tour.Culmination

© 2022 - 2024 — McMap. All rights reserved.