[Tutor] Clearing A DOS Screen

Scott Widney SWidney@ci.las-vegas.nv.us
Wed, 16 Oct 2002 08:01:07 -0700


> 
> Running a program outside of Pythonwin I want to clear the 
> DOS box ... I've tried using popen('cls') but it has no 
> effect ... any ideas?
> 

>From the documentation:
"""
popen(command[, mode[, bufsize]]) 
Open a pipe to or from command. The return value is an open file object
connected to the pipe, which can be read or written depending on whether
mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as
the corresponding argument to the built-in open() function. The exit status
of the command (encoded in the format specified for wait()) is available as
the return value of the close() method of the file object, except that when
the exit status is zero (termination without errors), None is returned.
Availability: Unix, Windows. 
"""

That's not what you want. You want os.system():
"""
system(command) 
Execute the command (a string) in a subshell. This is implemented by calling
the Standard C function system(), and has the same limitations. Changes to
posix.environ, sys.stdin, etc. are not reflected in the environment of the
executed command. The return value is the exit status of the process encoded
in the format specified for wait(), except on Windows 95 and 98, where it is
always 0. Note that POSIX does not specify the meaning of the return value
of the C system() function, so the return value of the Python function is
system-dependent. Availability: Unix, Windows. 
"""

Also, since os.system() returns a value (usually zero), you'll want to dump
that somewhere or it will show up as the first character on your newly
cleared screen. Try this:

>>> import os
>>> nil = os.system('cls')


Scott