How do I read something from DOS? For example, doing a command in DOS, like 'dir'/etc. and writing the output to a variable in python? I know about the listdir() function, 'dir' in DOS is just an example.

Hey nuaris,
I'm not sure why you would need DOS scripts when you could code everything using the full power of Python. Anyway, here is a really CRUDE and primitive bit of code that might help point you in the right direction. I'm assuming your code will start from a DOS script, and then call the Python script, and you want a way to pick up information from the DOS variables or DOS output??? BTW, I'm a Python newbie.

Why not use the DOS redirect / pipe command > to send the DOS output to a text file that you will give a unique name, and then have the Python script read / parse that specific text file? Very crude, I know. (And it only works on Windows; like DOS). :)

Here's what I tried.
Create a DOS batch script (my example is called "StartMe.bat"), and put in your DOS commands. My file contains 2 lines.

dir > myDirContents.txt
echo This is my silly text.  Please insert a sensible DOS command here for this DOS batch file.  > mySillyFile.txt

Put your Python code here

stringmyfile01 = open("mySillyFile.txt","r")
stringmyfile02 = open("myDirContents.txt","r")
Now parse / process the text file contents.

Good luck :)

Most of the DOS commands you are familiar with are contained in these two modules:

import os

os.chdir(path) change directory to the one in path 
os.curdir      return current directory
os.chmod(path, mode)  change the mode of path to the new mode
os.getcwd()  return the current working directory
os.mkdir(path[, mode])  create a directory named path with 
    numeric mode (default 0777)
os.remove(path) removes/deletes file given in path
os.rename(old, new) renames file or directory old to new
os.rmdir(path)  removes an empty directory path
os.walk(top[, topdown=True [, onerror=None]])  generates a 
    list of file names in a directory tree

import shutil
shutil.copy(src, dest)  copies the contents of file src to file dest
    retains file permissions
shutil.copytree(src, dest[, symlinks])  recursively copies an entire
    directory tree rooted at src into dest
shutil.move(src, dest) moves a file or directory to a new location
shutil.rmtree(path[, ignore_errors[, onerror]]) deletes an entire 
    directory tree

I need to read from DOS because I'm using the output from a command-line application. Thanks for the replies, they were really helpful.

This is approximately what I would do in your position. You really should read the appropriate documentation as there are tricky conditions that can arise when the program you run is looking for input, but this should get you started.

>>> import popen2
>>> cmdout , cmdin = popen2.popen2("dir")
>>> for line in cmdout: print line, # Trailing comma because newline already there
...
 Volume in drive C has no label.
 Volume Serial Number is C27B-EDA0

 Directory of C:\Python25

03/16/2008  01:10 PM    <DIR>          .
03/16/2008  01:10 PM    <DIR>          ..
04/12/2008  03:44 PM            23,484 basemap-wininst.log
03/16/2008  01:10 PM    <DIR>          DLLs
03/16/2008  01:10 PM    <DIR>          Doc
02/03/2008  04:26 PM             2,789 ffnet-wininst.log
03/16/2008  01:10 PM    <DIR>          include
08/03/2008  09:10 AM    <DIR>          Lib
03/16/2008  01:10 PM    <DIR>          libs
02/21/2008  01:05 PM            14,013 LICENSE.txt
03/08/2008  09:41 PM           105,852 matplotlib-wininst.log
02/03/2008  04:26 PM            29,575 networkx-wininst.log
02/21/2008  01:05 PM           119,048 NEWS.txt
02/21/2008  01:11 PM            24,064 python.exe
02/21/2008  01:12 PM            24,576 pythonw.exe
02/21/2008  01:05 PM            56,354 README.txt
04/12/2008  03:44 PM            61,440 Removebasemap.exe
02/03/2008  04:26 PM            61,440 Removeffnet.exe
03/08/2008  09:41 PM            61,440 Removematplotlib.exe
02/03/2008  04:26 PM            61,440 Removenetworkx.exe
02/03/2008  04:26 PM            61,440 Removescipy.exe
02/03/2008  04:26 PM            94,727 scipy-wininst.log
02/03/2008  04:29 PM    <DIR>          Scripts
02/03/2008  04:26 PM    <DIR>          share
03/16/2008  01:10 PM    <DIR>          tcl
03/16/2008  01:10 PM    <DIR>          Tools
02/21/2008  01:11 PM             4,608 w9xpopen.exe
              16 File(s)        806,290 bytes
              11 Dir(s) 855,117,389,824 bytes free
>>> cmdout.close() # Don't forget to close output and input handles
>>> cmdin.close()
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.