0

Hi I am trying to write the python script by which I can change my pwd to /usr/local/src but the problem is this by using below script again I am redirecting to my prevoius directory

import os 
def path_init(): 
  print os.getcwd() 
  os.chdir("/usr/local/src") 
  print os.getcwd() 

def main(): 
  path_init() 

if __name__ == 'main':
    main() 

output:

[root@localhost Desktop]# python p.py 
/root/Desktop 
/usr/local/src 

[root@localhost Desktop] 

1 Answer 1

3

Impossible. Pwd is a per-process attribute and the script will always be executed as a separate process; it cannot change the shell's pwd.

(Except, that is, by useless hacks such as

#!/usr/bin/env python
# effectively a poor man's echo(1)
import sys
print(sys.argv[1])

then

cd $(p.py)

.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.