Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Hiding the command prompt in my program.

Posted on 2008-06-22
9
Medium Priority
?
1,391 Views
Last Modified: 2010-04-21
Hello everyone,

I'm currently developing a tool in Python using wxPython for my GUI.
Now I'm using wx.Shell() to execute commands to the command prompt.
Now this all works fine, but the command prompts keep popping up, is there a way to hide these?

Here's a copy of the modules I imported so far.
import wx
import os, sys
import string
import win32api
import re

Regards,
Olrik
0
Comment
Question by:Artellos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21843085
what is it that you need to call a shell command, that Python cannot do? A third party application ?
0
 
LVL 3

Author Comment

by:Artellos
ID: 21843743
I'm probably doing it in a really inefficient way (feel free to tip about that.) but I'm using wx.Shell() from wxPython to call ipconfig / dxdiag and even notepad for example.

Regards,
Olrik
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21843849
what are you trying to get out of ipconfig ? the IP address? for cross platform scripts/application its best to use what Python already provides. eg

import socket
socket.gethostbyname(socket.gethostname())

since you are using wxPython, why not create a simple text editor yourself, instead of calling Notepad, which other platforms like Linux doesn't have.
0
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
LVL 3

Author Comment

by:Artellos
ID: 21844054
I'm making a tool that will only be used for Windows, Cross platform is not important to me.
I want to view everything an ipconfig /all shows, but ipconfig is not the only one.
Also "DxDiag /t <outputfile>" is run using a wx.Shell() in my program.

But I suppose there's no way to hide the command prompt?

Regards,
Olrik
0
 
LVL 29

Accepted Solution

by:
pepr earned 2000 total points
ID: 21844263
Well, I partly agree and partly disagree with ghostdog74. Some functionality may be available in the Python standard modules, and then it is better to use, say, os.rename() than to call console command to rename the file. On the other hand, working with shell (or cmd) is the most effective if you have a ready-to-be-used solution.

Until Python 2.4, the simplest way to execute the cmd command was to use os.system(). To avoid showing the console window, you could use os.popen() or the variants os.popen2(), os.popen3(), and popen4() -- depends on what standard files (stdout, stdin, stderr) you want to redirect. See http://docs.python.org/lib/os-newstreams.html#l2h-2626

More modern approach is to use the subprocess module (http://docs.python.org/lib/module-subprocess.html -- see the section on how to replace older os.system() and os.popen() commands)  and its Popen() (http://docs.python.org/lib/node528.html#l2h-3628). You can also use the subprocess.call() helper function.

Notice the default shell=True argument. Then the command is executed via the OS shell. You can also avoid it, but then you have to tell more exact form of arguments (full paths, no "automatic" replacement of shell variable names,...).
0
 
LVL 3

Author Comment

by:Artellos
ID: 21844491
I guess it's a dumb question. (I'm still learning everything in python.)

How would I go about running an ipconfig, save the output to C:\ipconfig.txt without a shell?

I come as far as "subprocess.Popen('ipconfig /all > C:\ipconfig.txt', shell=False)"
However this appears to still show a command screen, what am I doing wrong? ;)

Regards,
Olrik
0
 
LVL 3

Author Comment

by:Artellos
ID: 21844520
I think I'm going to use the older fashion way using os.popen()
Unless there's a really strong argument not to use it ofcourse!

So far os.popen() has worked for me.

Regards,
Olrik
0
 
LVL 29

Expert Comment

by:pepr
ID: 21845245
You can try the following testing script that uses subprocess...
import subprocess
 
fout = open('ipconfig.txt', 'w')
subprocess.Popen('ipconfig /all', stdout=fout)
fout.close()

Open in new window

0
 
LVL 3

Author Closing Comment

by:Artellos
ID: 31469586
Thanks for the help! :)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question