how can I implement "cd" like shell in Python?

S

Sergi Pasoev

Do you mean to implement the cd command ? To what extent do you want to
implement it ? if what you want is just to have a script to change the
current working directory, it is as easy as this:


import sys
import os
os.chdir(sys.argv[1])

plus you could add some error-handling code.
 
H

Hans Mulder

Do you mean to implement the cd command ? To what extent do you want to
implement it ? if what you want is just to have a script to change the
current working directory, it is as easy as this:


import sys
import os
os.chdir(sys.argv[1])

plus you could add some error-handling code.

'cd' is a shell built-in, because otherwise it would have no effect.

You can write a Python script that invokes os.chdir(), but that won't
have any effect on the shell that starts the script.

What are you trying to achieve?

-- HansM
 
L

Laurent Pointal

Sergi said:
Do you mean to implement the cd command ? To what extent do you want to
implement it ? if what you want is just to have a script to change the
current working directory, it is as easy as this:


import sys
import os
os.chdir(sys.argv[1])

plus you could add some error-handling code.

To have a shell / python script interaction for cwd management, you can take
a look at autojump

https://github.com/joelthelion/autojump/wiki

Its a Python script + different shells glue.

A+
Laurent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top