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?

Recommended Answers

All 5 Replies

indeed it is possible, very easy actually

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

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

import os
while True:
   command = raw_input("Enter Command->")
   if command.upper() == "Q":
      break
   try:
      os.system(command)
   except:
      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

import os
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

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?

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")?

You should look into the os module as i suggested as this allows you to do alot of things. here is an example

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

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

for path, dirs, files in os.walk("C:"):
   for f in files:
      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

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:

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

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

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.