For say in terminal I did cd Desktop
you should know it moves you to that directory, but how do I do that in python but with use Desktop with raw_input("")
to pick my command?

- 9,298
- 2
- 35
- 55
-
Are you asking how to perform shell/cmd commands via raw_input in Python? – Chuck Mar 08 '16 at 05:49
-
chuck logan that is correct thank you for knowing understanding my question – Mar 08 '16 at 05:51
-
Chuck Logan i checked it out there is no sign of raw_input as i see – Mar 08 '16 at 05:57
-
Are you using python 2 or 3? `raw_input` from python 2 is now `input` in python 3. – tdelaney Mar 08 '16 at 07:00
3 Answers
The following code reads your command using raw_input, and execute it using os.system()
import os
if __name__ == '__main__':
while True:
exec_cmd = raw_input("enter your command:")
os.system(exec_cmd)
Best Regards, Yaron

- 10,166
- 9
- 45
- 65
-
-
See the following Q&A which deals with "cd" http://stackoverflow.com/questions/35843054/change-directory-in-terminal-using-python/35843532#35843532 – Yaron Mar 08 '16 at 06:11
-
-
As mentioned here: http://stackoverflow.com/questions/35843054/change-directory-in-terminal-using-python/35843532#35843532 - when executing this code, with "cd /home" - the directory is changed while python is running. After python process ends, you receive the original shell with the original current-directory. – Yaron Mar 08 '16 at 06:22
-
-
Which problem are you trying to solve? Did you read the entire Q&As here: http://stackoverflow.com/questions/35843054/change-directory-in-terminal-using-python/35843532#35843532 – Yaron Mar 08 '16 at 06:32
-
-
The code in the accepted answer in Yaron's link launches a new bash process in the new directory so that when the Python script ends you are still in the new directory. If you end that process with ^D (or `exit`) you will be returned to the previous bash process. – PM 2Ring Mar 08 '16 at 06:38
-
maybe you can do this:
>>> import subprocess
>>> input = raw_input("")
>>> suprocess.call(input.split()) # for detail usage, search subprocess
for details, you can search subprocess
module

- 16,802
- 32
- 96
- 149
-
This will throw an error in the case of `input = "ls -l"` as shown in the `subprocess` docs. You need to `split` the input string by the `space` character in order to give `subprocess.call()` something it can work with (a list). – sulimmesh Mar 08 '16 at 06:39
-
To go with your specific example, you'd do the following:
import os
if __name__ == "__main__":
directory = raw_input("Please enter absolute path: ")
old_dir = os.getcwd() #in case you need your old directory
os.chdir(directory)
I've used this technique before in some directory maintenance functions I've written and it works. If you want to run shell commands more generally you'd something like:
import subprocess
if __name__ == "__main__":
command_list = raw_input("").split(" ")
ret = subprocess(command_list)
#from here you can check ret if you need to
But beware with this method. The system here has no knowledge about whether it's passing a valid command, so it's likely to fail and miss exceptions. A better version might look like:
import subprocess
if __name__ == "__main__":
command_kb = {
"cd": True,
"ls": True
#etc etc
}
command_list = raw_input("").split(" ")
command = command_list[0]
if command in command_kb:
#do some stuff here to the input depending on the
#function being called
pass
else:
print "Command not supported"
return -1
ret = subprocess(command_list)
#from here you can check ret if you need to
This method represents a list of supported commands. You can then manipulate the list of args as needed to verify it's a valid command. For instance, you can check if the directory you're about to cd
exists and return an error to the user if not. Or you can check if the path name is valid, but only when joined by an absolute path.

- 693
- 1
- 6
- 23