Command Line in Python

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Re: Command Line in Python

Post by the0nlyb0ss on Fri Sep 24, 2010 8:43 pm
([msg=46444]see Re: Command Line in Python[/msg])

Yeah, my school blocks it too :cry:
I re-did the code for Python 2.6, feel free to do whatever you want with it.
Code: Select all
import os, subprocess

while True:
   current = os.getcwd()
   print ''
   command = raw_input(current + '>')
   if command.upper() == 'EXIT':
      break
   if command.find('cd') != -1:
      command = command.replace('cd ', '')
      os.chdir(command)
   else:
      data = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).communicate()
      data = data[0]
      print data


Compared to your original program, I think this is a much better approach to things, especially since os.system is obsolete; use subprocess.Popen() instead, happy programming :mrgreen:
"Knowledge is knowing that a tomato is a fruit, but Wisdom is knowing not to put it in a fruit salad."
User avatar
the0nlyb0ss
Experienced User
Experienced User
 
Posts: 54
Joined: Thu Sep 02, 2010 11:24 pm
Location: California
Blog: View Blog (0)


Re: Command Line in Python

Post by Draymire on Tue Sep 28, 2010 10:09 am
([msg=46672]see Re: Command Line in Python[/msg])

Thanks for the code i will mess around with it and see what else i can come up with.
Baba Ram Dass "The quieter you become, the more you can hear"
User avatar
Draymire
Poster
Poster
 
Posts: 129
Joined: Sun Nov 22, 2009 12:01 am
Blog: View Blog (0)


Previous

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests