-1

I'm writing a tool to make my jump convenient. After executing following command,

./main.py  /etc

the current path is actually jump to /etc.

If I use os.chdir in main.py, then the current path would pop to the original path which is no what I expected.

How can I change the real current path in python ?

hao li
  • 367
  • 2
  • 13
  • 2
    You *are* changing "the real current path"... of your Python process. Not your shell's current directory. Only your shell can change its own directory. – user2357112 Jul 06 '20 at 07:21

1 Answers1

2

Your shell and your Python script are running as two different processes. Each process have a distinct working directory. Changing the working directory in one process does not effect the working directory of any other process.

That's why the shell cd command can't be created as an external program.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • So there is no way to custom a `cd` command? – hao li Jul 06 '20 at 07:24
  • @haoli No there isn't. If you need a "custom" `cd` command, then you could create it as a function in the shell to perform whatever other actions you might need, then invoke the shells built-in `cd` command. – Some programmer dude Jul 06 '20 at 07:30
  • Well, [*you could*](https://github.com/robertswiecki/extcd) bully the target program into `cd`ing to a different folder using `ptrace(2)`... but I would not suggest it for any reason other than having fun and exploring new interesting ways of breaking your system. – Marco Bonelli Jul 06 '20 at 07:51