473,383 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Clearing a DOS terminal in a script

This doesn't seem to work in a dos terminal at the start of a script:

from os import popen
print popen('clear').read()

Any idea why not? Thanks.

Stephen
Dec 13 '07 #1
5 2332
On Dec 13, 2007 10:48 AM, Stephen_B <st*****@theboulets.netwrote:
This doesn't seem to work in a dos terminal at the start of a script:

from os import popen
print popen('clear').read()

Any idea why not? Thanks.

It opens "clear" with it's own virtual terminal and clears that
instead. There's an ANSI control code you can use to reset the screen,
try printing that.
Dec 13 '07 #2
On Dec 13, 11:21 am, "Chris Mellon" <arka...@gmail.comwrote:
It opens "clear" with it's own virtual terminal and clears that
instead.
Even when I launch the script from a cmd shell with "python
myscript.py"?
There's an ANSI control code you can use to reset the screen, try printing that.
I googled "<Esc>[2J" as an ascii sequence that it is supposed to clear
the screen. How do I send that?

Stephen
Dec 13 '07 #3
On Dec 14, 3:48 am, Stephen_B <step...@theboulets.netwrote:
This doesn't seem to work in a dos terminal at the start of a script:

from os import popen
print popen('clear').read()

Any idea why not? Thanks.
Maybe you are using a different "dos terminal". What is "clear"?

C:\junk>clear
'clear' is not recognized as an internal or external command,
operable program or batch file.

C:\junk>ver

Microsoft Windows XP [Version 5.1.2600]

C:\junk>help cls
Clears the screen.

CLS

C:\junk>

The following works for me:

<code>
import os
os.system('cls')
print 'Howzat?'
</code>

To use ANSI escape sequences with the "real" MS-DOS, one needed a
console device driver (ANSI.SYS was supplied) and had to invoke it in
the CONFIG.SYS file; presumably you can find out how to set this up in
a Windows Command Prompt window, but it doesn't seem to be worth the
bother if all you want to do is clear the window.

HTH,
John
Dec 13 '07 #4
Stephen_B wrote:
This doesn't seem to work in a dos terminal at the start of a script:

from os import popen
print popen('clear').read()

Any idea why not? Thanks.
As others have mentioned, you should just do:

os.system("cls")

Or, you can use my WConio module for fancier work.

http://newcenturycomputers.net/projects/wconio.html

Good luck!

-- Chris.

Dec 14 '07 #5
Stephen_B wrote:
On Dec 13, 11:21 am, "Chris Mellon" <arka...@gmail.comwrote:
>It opens "clear" with it's own virtual terminal and clears that
instead.

Even when I launch the script from a cmd shell with "python
myscript.py"?
>There's an ANSI control code you can use to reset the screen, try printing that.

I googled "<Esc>[2J" as an ascii sequence that it is supposed to clear
the screen. How do I send that?

Stephen
Normally you would do:

import sys
sys.stdout.write(chr(27)+'[2J')
sys.stdout.flush()

Unfortunately that doesn't clear the screen because the ANSI module isn't loaded
by default. Use os.system('cls') as mentioned instead.

-Larry
Dec 16 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dan Richert | last post by:
i wrote a script that accesses files at random from the locatedb database. it then prints a random line from the file it's accessed to the terminal screen. this runs continuously and at times...
6
by: Carol Pedder | last post by:
Hello all, may I ask a question? Having just started C++ (using microsoft visual studio), I am using cin/cout for console applicarions (yes I know it doesn't happen in the real world!) and I'm...
2
by: egbert | last post by:
When I start the following script in a gnome-terminal: #!/usr/bin/env python import os print "hello gnome-terminal" print os.environ I see the expected results in the same gnome-terminal...
10
by: mwt | last post by:
I'm doing some python programming for a linux terminal (just learning). When I want to completely redraw the screen, I've been using os.system("clear") This command works when using python in...
16
by: John Salerno | last post by:
Here's my new project: I want to write a little script that I can type at the terminal like this: $ scriptname package1 where scriptname is my module name and any subsequent arguments are the...
65
by: Leslie Kis-Adam | last post by:
Hi everyone! Does anyone know, if it is possible to clear the screen in ANSI C? If it is,then how? Any help would be appreciated. Laszlo Kis-Adam <dfighter_AT-NOSPAM_freemail.hu
2
by: David | last post by:
Hi list. I've never used unicode in a Python script before, but I need to now. I'm not sure where to start. I'm hoping that a kind soul can help me out here. My current (almost non-existant)...
0
by: hce | last post by:
Hi, I am running Python and a script on XP DOS terminal, the script build.py printed out messages on the Terminal, then asked for an input of numbers. But, I pressed a key, nothing came in DOS...
0
by: Gabriel Genellina | last post by:
En Fri, 09 May 2008 01:42:21 -0300, hce <webmail.hce@gmail.comescribió: Next time try with Ctrl-Break. It's hard to say what's wrong without seeing the script. -- Gabriel Genellina
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.