471,344 Members | 992 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Popen question with opening and closing

I am finishing some sections on my code and I had a question on a method.

I will open a file that brings up a dos prompt using a python script. My question is, how do I pass information from my python script to this dos prompt after python has opened it? The information will just be a text string with a carriage return or a simple command or two.

Thanks alot,
JP
Jun 13 '07 #1
17 2907
ghostdog74
511 Expert 256MB
how is your script like?
Jun 14 '07 #2
bartonc
6,596 Expert 4TB
I am finishing some sections on my code and I had a question on a method.

I will open a file that brings up a dos prompt using a python script. My question is, how do I pass information from my python script to this dos prompt after python has opened it? The information will just be a text string with a carriage return or a simple command or two.

Thanks alot,
JP
My guess is that you are contemplating this machination because you want/need a GUI type of display (or perhaps, input). But that doesn't make sense to me because python has a fine shell window to display and read strings to/from the user. I may be way off track here or you may want to rethink the problem a bit. Please clarify for me you reasoning. Thanks.
Jun 14 '07 #3
Thanks for the response guys.

Anyways, to clarify the code:

There is a gui that passes information into another segment of code that creates an input deck for a program. To start this program and utilize the input deck, I execute an exe/batch file that brings up an ms-dos command prompt with preloaded libraries from executing the batch file. The purpose is to execute this file in the script and pass a few commands to the dos prompt that gets opened up.

I appologize that I cannot post the script as it containes contract work. Any help is greatly appreciated! Also just ask more if clarification is needed.

Thanks,
JP
Jun 15 '07 #4
bartonc
6,596 Expert 4TB
Thanks for the response guys.

Anyways, to clarify the code:

There is a gui that passes information into another segment of code that creates an input deck for a program. To start this program and utilize the input deck, I execute an exe/batch file that brings up an ms-dos command prompt with preloaded libraries from executing the batch file. The purpose is to execute this file in the script and pass a few commands to the dos prompt that gets opened up.

I appologize that I cannot post the script as it containes contract work. Any help is greatly appreciated! Also just ask more if clarification is needed.

Thanks,
JP
Search the docs for "popen". You'll find lots of Windows related stuff there.
Jun 15 '07 #5
Thanks B,

I'll start looking at the popen module.

JP
Jun 15 '07 #6
Well I got the file to open with this mini script.

# This module experiments with python pointing to a command prompt from MCNP5.
Expand|Select|Wrap|Line Numbers
  1. from os import *
  2. from sys import *
  3. import subprocess
  4.  
  5. subprocess.Popen(r"C:\cygwin\usr\X11R6\bin\startxwin.bat")
  6. subprocess.Popen(r"C:\WINDOWS\system32\cmd.exe")
  7.  
#########################

The subprocess opens a command prompt that stays up. How do I feed a few text commands such as "cd directory [enter]" and then close the prompt?

Thanks again!
JP
Jun 15 '07 #7
bartonc
6,596 Expert 4TB
Well I got the file to open with this mini script.

# This module experiments with python pointing to a command prompt from MCNP5.

from os import *
from sys import *
import subprocess

subprocess.Popen(r"C:\cygwin\usr\X11R6\bin\startxw in.bat")
subprocess.Popen(r"C:\WINDOWS\system32\cmd.exe")

#########################

The subprocess opens a command prompt that stays up. How do I feed a few text commands such as "cd directory [enter]" and then close the prompt?

Thanks again!
JP
Hey, JP, we've got to get you to start using [code] tags. Instructions are on the right hand side while you reply or post. When your editor has the Enhaced Mode button selected, the # button will write them for you.
Thanks.
Jun 15 '07 #8
bartonc
6,596 Expert 4TB
Well I got the file to open with this mini script.

# This module experiments with python pointing to a command prompt from MCNP5.
Expand|Select|Wrap|Line Numbers
  1. from os import *
  2. from sys import *
  3. import subprocess
  4.  
  5. subprocess.Popen(r"C:\cygwin\usr\X11R6\bin\startxwin.bat")
  6. subprocess.Popen(r"C:\WINDOWS\system32\cmd.exe")
  7.  
#########################

The subprocess opens a command prompt that stays up. How do I feed a few text commands such as "cd directory [enter]" and then close the prompt?

Thanks again!
JP
a popen() process work like a file with read and write function. I haven't used it, so you'll need to read the documentation.
Also, I haven't looked at the subprocess module at all.

Basically, though, you want a reference to a subprocess object that you can call read() and write() methods on.
Jun 15 '07 #9
Thanks alot!

Looking more into popen then.

JP
Jun 15 '07 #10
Hi everyone,

I figured out how to open and pass information into a command prompt. Though it runs as a ghost process. Here is an example for posterity!

Expand|Select|Wrap|Line Numbers
  1. from os import *
  2. from sys import *
  3. import subprocess
  4. from popen2 import *
  5.  
  6. output, input = popen2(r"C:\WINDOWS\system32\cmd.exe")
  7.  
  8. input.write("cd C:\ \n")
  9. input.write("mcnp5 ip i=mcnp.txt\n")
  10. input.close()
  11. print output.read()


My question is a little different this time. I want to start an Xwin process in my script if it is not currently running. If it is running I will pass without starting it. Any idea how to test if a process is running in the background?

Here is a pseudocode idea:

if process==false:
popen(r'C:\Directoryandprogram')
else:
#####pass through

Thanks for the help,
JP
Jun 15 '07 #11
bartonc
6,596 Expert 4TB
Hi everyone,

I figured out how to open and pass information into a command prompt. Though it runs as a ghost process. Here is an example for posterity!

Expand|Select|Wrap|Line Numbers
  1. from os import *
  2. from sys import *
  3. import subprocess
  4. from popen2 import *
  5.  
  6. output, input = popen2(r"C:\WINDOWS\system32\cmd.exe")
  7.  
  8. input.write("cd C:\ \n")
  9. input.write("mcnp5 ip i=mcnp.txt\n")
  10. input.close()
  11. print output.read()


My question is a little different this time. I want to start an Xwin process in my script if it is not currently running. If it is running I will pass without starting it. Any idea how to test if a process is running in the background?

Here is a pseudocode idea:

if process==false:
popen(r'C:\Directoryandprogram')
else:
#####pass through

Thanks for the help,
JP
Thanks for the sample. I'm sure that this will be a great help to fellow members (including me).

There are extensions for working on Windows that allow you to do all types of system queries. I use win32 stuff often and the ctypes module comes in very handy for (say) calling system DLL functions. But for your purposes, I think that you'll probably want to check out WMI.
Jun 15 '07 #12
ghostdog74
511 Expert 256MB
Hi everyone,

I figured out how to open and pass information into a command prompt. Though it runs as a ghost process. Here is an example for posterity!

Expand|Select|Wrap|Line Numbers
  1. from os import *
  2. from sys import *
  3. import subprocess
  4. from popen2 import *
  5.  
  6. output, input = popen2(r"C:\WINDOWS\system32\cmd.exe")
  7.  
  8. input.write("cd C:\ \n")
  9. input.write("mcnp5 ip i=mcnp.txt\n")
  10. input.close()
  11. print output.read()


My question is a little different this time. I want to start an Xwin process in my script if it is not currently running. If it is running I will pass without starting it. Any idea how to test if a process is running in the background?

Here is a pseudocode idea:

if process==false:
popen(r'C:\Directoryandprogram')
else:
#####pass through

Thanks for the help,
JP
you don't have to call external cmd.exe and then use it to call mcnp5. you can use subprocess/popen to call it straightaway.
As for checking of process, there are 3 ways i can think of.
1) Use an external command like taskkill.
2) Use the win32 extensions . In the win32 scripts directory, search for killProcName.py it shows you example of how to kill process.
3) Use WMI. see here for example
Jun 16 '07 #13
Thanks guys,

I'll post a snippet of what I can when I figure it out. Workin on the WMI stuff.

JP
Jun 16 '07 #14
Earlier had a problem but solved by a restart, continuing the wmi experiment!
Jun 16 '07 #15
Heres a code snippet for all of you gents and ladies. As always thanks for the help.

Expand|Select|Wrap|Line Numbers
  1. ########################
  2. #This snippet starts Xwin if it is not running, else wise it passes.
  3. #The main purpose is that Xwin crashes if multiple instances are opened.
  4. #Used Tim Golden's Cookbook as advice
  5. #######################
  6. from os import *
  7. from sys import *
  8. import subprocess
  9. from popen2 import *
  10. import wmi
  11. c = wmi.WMI()
  12. counter=0
  13. for process in c.Win32_Process():
  14.     if str(process.Name)=="XWin.exe":
  15.         print "Found Xwin"
  16.         counter=1
  17.     else:
  18.         print "Some other program"
  19.  
  20. if counter==0:
  21.     print "Prepare to start XWin"
  22.     subprocess.Popen(r"C:\cygwin\usr\X11R6\bin\startxwin.bat")
  23.  
JP
Jun 16 '07 #16
jlm699
314 100+
Well I got the file to open with this mini script.

# This module experiments with python pointing to a command prompt from MCNP5.
Expand|Select|Wrap|Line Numbers
  1. from os import *
  2. from sys import *
  3. import subprocess
  4.  
  5. subprocess.Popen(r"C:\cygwin\usr\X11R6\bin\startxwin.bat")
  6. subprocess.Popen(r"C:\WINDOWS\system32\cmd.exe")
  7.  
#########################

The subprocess opens a command prompt that stays up. How do I feed a few text commands such as "cd directory [enter]" and then close the prompt?

Thanks again!
JP
I am attempting to run an external program from within my Python script, however it is not a shell command or a batch script. It is actually an installer.exe but I cannot find any documentation or forum posts about running an actual windows program (It would be nice to be able to run this program in either Windows or Linux). The application that I'm developing is written in Python 2.4 and uses wxPython for GUI, among a large assortment of modules from psycopg2 to paramiko.
Since this application has gone through many iterations and likely will continue to do so in the future as bugs are found, I've implemented the following version check:
Expand|Select|Wrap|Line Numbers
  1.     # Check for newer versions from ftp://www.testsite.com
  2.     from ftplib import FTP
  3.     latest_version = []
  4.     try:
  5.         ftp = FTP('www.testsite.com', 'user.testsite.com', 'passwd')
  6.         ftp.cwd('Test/foobar')
  7.         ftp.retrlines('LIST', latest_version.append)
  8.         for file in latest_version:
  9.             if re.search('installfoo', file):
  10.                 latest_version = file.split()[-1]
  11.         latest_version = latest_version.split('-')[1]
  12.  
  13.         if latest_version > Global.myversion:
  14.             Display_Version_Message()
  15.     except:
  16.         print formatExceptionInfo()
  17.  
Btw, the splitting done with the file name is because the filename is of the format installfoo-0.9.4-installer.exe, and Display_version_message() is just a simple wx.MessageDialog. However, instead of displaying a message that says, You have an old version, update to the latest I was hoping to just download the installer to their machine, and execute it. I tried doing
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. stdin, stdout = os.popen2('C:\\Program Files\\installfoo.exe')
  4.  
but this just returns the file-like objects, which I don't know what to do with next. The installer file that I speak of is created via a NSIS script, which when compiled makes the .exe file. I use py2exe to create the /build and /dist directories also if that matters.

If anybody has any ideas on how to just simply execute an external program and let it do it's thing, I would greatly appreciate hearing (reading) them.

Thanks,
James
Aug 28 '07 #17
jlm699
314 100+
Ok, scratch my last post... I just have to use
Expand|Select|Wrap|Line Numbers
  1. os.system('CMD /C ' + program_to_execute)
  2.  
However now I'm wondering if there is either a way to perform os.system without waiting for the exit status, or if there is a similar command that proceeds without waiting for the exit status.

Anybody know of anything like that? Thanks in advance
Aug 28 '07 #18

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Mark Roach | last post: by
reply views Thread by Kinsley Turner | last post: by
1 post views Thread by rveloso | last post: by
7 posts views Thread by Martien van Wanrooij | last post: by
2 posts views Thread by hubritic | last post: by
1 post views Thread by Jean-Paul Calderone | last post: by
25 posts views Thread by Jeremy Banks | last post: by

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.