Crazy question -- Python + Command Line

Reply

Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

Crazy question -- Python + Command Line

 
0
  #1
May 21st, 2008
This is probably totally absurd, but...is it possible to, through Python, access the command line and use it? For instance, write a program that outputted all of the files in a given directory, much like

cd [directory]
dir

would?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Crazy question -- Python + Command Line

 
0
  #2
May 21st, 2008
indeed it is possible, very easy actually

  1. import os
  2. os.system("cd [directory]")

and if you want to make it so that you can type commands into your python commandline

  1. import os
  2. while True:
  3. command = raw_input("Enter Command->")
  4. if command.upper() == "Q":
  5. break
  6. try:
  7. os.system(command)
  8. except:
  9. print "invalid command\n"

One thing to note is that if you are just going to make specific calls to the command line to do one thing such as cd directory, you should use the python os module rather than just calling the comman shell.

To perform a cd command from python you ould use the following

  1. import os
  2. os.chdir("C:\\A folder")

This changes the current working directory to C:\Folder. From there you cann call other functions to make directories etc.

Python: os module or type help("os") in the Python command line

you should also look at os.name and os.path especially, help("os.name") etc for information on these.

Hope this helps, just be careful if you do allow user input to access the command prompt.

Chris
Last edited by Freaky_Chris; May 21st, 2008 at 5:20 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

Re: Crazy question -- Python + Command Line

 
0
  #3
May 21st, 2008
That helps very much, thanks! One last thing, is it possible for Python to read the output of the commands it executes? For instance, storing the contents of a directory in a list?
Last edited by FreezeBlink; May 21st, 2008 at 1:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

Re: Crazy question -- Python + Command Line

 
0
  #4
May 21st, 2008
Sorry--I just realized that wasn't worded very clearly. What I mean is this:

Is it possible for Python to store the output of calling os.system([command])? For instance, is it possible to store, in a list, all the results of os.system("dir")?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Crazy question -- Python + Command Line

 
0
  #5
May 22nd, 2008
You should look into the os module as i suggested as this allows you to do alot of things. here is an example

  1. import os
  2. for path, dirs, files in os.walk("C:"):
  3. print path, dir, files

ofcourse this is a very basic example and can be expanded greatly. You can loop through files

  1. for path, dirs, files in os.walk("C:"):
  2. for f in files:
  3. print f

so as you can see this way you can do alot of things this way. You can store the files use them to do other things with whatever.

Chris
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: rikxik is an unknown quantity at this point 
Solved Threads: 3
rikxik rikxik is offline Offline
Newbie Poster

Re: Crazy question -- Python + Command Line

 
0
  #6
May 23rd, 2008
You should indeed use existing/available python modules as you have many built-in functions available for exactly what you need instead of having to sift through output to do something specific. Using python modules instead of calling os-specific commands keeps your programs portable and they perform better.

However, just in case you wish to capture output from external commands, you can use popen:

  1. import os
  2. p = os.popen("dir")
  3. for i in p:
  4. print i.strip() # prints the dir output one line at a time
  5. p.close()

There is module called "commands" which provides a nice wrapper around popen but I have not tried it on Windows.

HTH
Last edited by rikxik; May 23rd, 2008 at 4:58 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1713 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC