473,246 Members | 1,399 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,246 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 2998
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

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

Similar topics

3
by: Mark Roach | last post by:
I am using the daemonize (double-fork) code from the cookbook, and have bumped into a strange issue. This is what my program is doing: def main(): ... useful things ... os.popen('/usr/bin/lp -d...
4
by: gonzal | last post by:
Hi Dose any body know why a temporary table gets deleted after querying it the first time (using SELECT INTO)? When I run the code bellow I'm getting an error message when open the temp table...
0
by: Kinsley Turner | last post by:
Hey, Does os.popen() actually work in a py2exe win32 package? As far as I can tell, it just doesn't seem to do anything, although it seems to work ok outside of py2exe. Any hints? There...
1
by: rveloso | last post by:
Hi all, i'm having a really nasty problem with popen. I have the following code : --------------------- .... FILE *PD; .... sprintf(fname,"/usr/bin/gzip -dc %s/%s",dirname,dp->d_name); .......
7
by: Martien van Wanrooij | last post by:
I have been faced a couple of times with the situation that I wanted to write a script and was worried about a too frequent opening and closing mysql connections. To give some examples: 1)I...
2
by: hubritic | last post by:
I am trying to set off commands on Windows 2003 from python. Specifically, I am trying to use diskpart with a script file (pointed to with path). cmd = p = Popen(cmd, shell=True) The script...
1
by: Jean-Paul Calderone | last post by:
On Mon, 12 May 2008 17:35:44 -0700 (PDT), schickb <schickb@gmail.comwrote: Alas, you haven't actually closed src's standard input. Though you did close src.stdin, you didn't close the copy of it...
2
by: rparimi | last post by:
I am trying to redirect stderr of a process to a temporary file and then read back the contents of the file, all in the same python script. As a simple exercise, I launched /bin/ls but this doesn't...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.