2

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?

danidee
  • 9,298
  • 2
  • 35
  • 55

3 Answers3

1

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

Yaron
  • 10,166
  • 9
  • 45
  • 65
  • When using the code cd wont work –  Mar 08 '16 at 06:05
  • 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
  • When i open the file and do cd /home/pi/Desktop/asf1/dos nothing happens –  Mar 08 '16 at 06:19
  • 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
  • Sorry about this but how would i make it not end? –  Mar 08 '16 at 06:25
  • 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
  • So in the code you sent me after one command the process ends –  Mar 08 '16 at 06:33
  • 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
  • How do i stop the script from ending after a command is executed? –  Mar 08 '16 at 06:51
1

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

GoingMyWay
  • 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
  • @skeletalbassman, yes, thanks. – GoingMyWay Mar 08 '16 at 07:40
1

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.

sulimmesh
  • 693
  • 1
  • 6
  • 23