How can I use the command (cd ..) in python

1

I have made a program in python client-server but I can not use the command in cd in shell mode which are the commands I use for msdos. I have been told with threads you can do I have used this:

x = input("introduce el comando: ")
subprocess.call([x, ''], shell=True)
subprocess.call('cd ..', shell=True)

With a while loop I repeat the sequences and I'm entering the command that I want. but the cd never does it to me.

use a function that simulates the command cd ..

to go back in directory (this will give effect of cd ..)

def back():
    path=os.getcwd()
    print path
    s=path.split('\')
    length=len(s)
    x=0

    while x<(length-1):
        if x==0:
            back_path=s[x]+"\"
        else:
            back_path=back_path+s[x]+"\"
        x+=1

    os.chdir(back_path)

How can I implement in my program?

python the worst language I've seen can not even use the cd .. .....

    
asked by Perl 01.08.2016 в 15:57
source

2 answers

2

The problem is that subprocess.call starts a new process, and it is this process that changes the directory and not the process of your script.

To change directory, the current process uses os.chdir :

os.chdir("..")
    
answered by 01.08.2016 в 16:46
1

I think you have to import the library os : import os And then you have to call the function os.system which is the one that executes the command, then it would look like this: os.system("cd ..")

    
answered by 01.08.2016 в 17:02