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
user3008712
6111 gold badge6 silver badges4 bronze badges
1 Answer 1
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
Fred Foo
365k80 gold badges765 silver badges852 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py