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] 
Richard
3,1501 gold badge20 silver badges35 bronze badges
asked Nov 19, 2013 at 12:28

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)

.)

answered Nov 19, 2013 at 12:48
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.