[Tutor] Running a dos program with python
Hello all, This is my first post to the Tutor@python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(rc:\shake91.txt) but cannot do the rest. When I run my script import os os.system(rc:\shake91.exe) In the IPython(x,y) console I see: -- * * SHAKE -- A COMPUTER PROGRAM FOR EARTHQUAKE RESPONSE * * ANALYSIS OF HORIZONTALLY LAYERED SITES * * by: Per B. Schnabel John Lysmer -- 1970 * * --- * * shake85 IBM-PC version of SHAKE * * by: S.S. (Willie) Lai, January 1985 * * --- * * shake88 : New modulus reduction curves for clays added* * using results from Sun et al (1988) * * by: J. I. Sun Ramin Golesorkhi* * February 26, 1988 * * --- * * SHAKE90/91: Adjust last iteration; Input now is either * * Gmax or max Vs; up to 13 material types can * * be specified by user; up to 50 Layers can * * be specified; object motion can be read in * * from a separate file and can have user * * specified format; Different periods for * * response spectral calculations; options * * are renumbered; and general cleanup * * by: J. I. Sun, I. M. Idriss P. Dirrim * * June 1990 - February 1991 * * --- * * SHAKE91 : General cleanup and finalization of input/ * * output format ... etc * * by: I. M. Idriss* * December 1991 * *** Name of Input File = -- And there is a blinking cursor after Name of Input File. At this point I can manually enter in the file name (and the two other remaining file names) and then press enter and the program does run. What I really want though is to be able to do the whole thing with a python script. Any ideas? Thanks Richie ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a dos program with python
On Wed, Mar 10, 2010 at 3:20 PM, Armstrong, Richard J. rarms...@water.ca.gov wrote: Hello all, This is my first post to the Tutor@python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(r”c:\shake91.txt”) but cannot do the rest. Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a dos program with python
From: sri...@gmail.com [mailto:sri...@gmail.com] On Behalf Of Wayne Werner Sent: Wednesday, March 10, 2010 2:24 PM To: Armstrong, Richard J. Cc: tutor@python.org Subject: Re: [Tutor] Running a dos program with python On Wed, Mar 10, 2010 at 3:20 PM, Armstrong, Richard J. rarms...@water.ca.gov wrote: Hello all, This is my first post to the Tutor@python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(rc:\shake91.txt) but cannot do the rest. Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne Wayne, It kindof works. I wrote subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt']) The dos program pops up and if I hit the enter key three times then it runs. How can I add these three enters into the script? Thanks, Richie ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a dos program with python
On Wed, Mar 10, 2010 at 4:51 PM, Armstrong, Richard J. rarms...@water.ca.gov wrote: The dos program pops up and if I hit the enter key three times then it runs. How can I add these three “enters” into the script? I'm not at all sure if this way would work, but you could send the \r\n through a pipe: p = subprocess.Popen([file1, file2, file3], stdin=subprocess.PIPE) p.communicate(\r\n\r\n\r\n) # Three windows line ending sequences. it also may be possible to add them to the end of the last parameter: 'b.txt\r\n\r\n\r\n' I don't have much faith that it will work, but you can certainly try! HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a dos program with python
I use wxPython, which allows a statement like: wx.Execute('c:\shake91.exe FLAC.txt a.txt b.txt') From: Armstrong, Richard J. Sent: Wednesday, March 10, 2010 4:51 PM To: Wayne Werner Cc: tutor@python.org Subject: Re: [Tutor] Running a dos program with python From: sri...@gmail.com [mailto:sri...@gmail.com] On Behalf Of Wayne Werner Sent: Wednesday, March 10, 2010 2:24 PM To: Armstrong, Richard J. Cc: tutor@python.org Subject: Re: [Tutor] Running a dos program with python On Wed, Mar 10, 2010 at 3:20 PM, Armstrong, Richard J. rarms...@water.ca.gov wrote: Hello all, This is my first post to the Tutor@python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(rc:\shake91.txt) but cannot do the rest. Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne Wayne, It kindof works. I wrote subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt']) The dos program pops up and if I hit the enter key three times then it runs. How can I add these three enters into the script? Thanks, Richie ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a dos program with python
From: sri...@gmail.com [mailto:sri...@gmail.com] On Behalf Of Wayne Werner Sent: Wednesday, March 10, 2010 3:00 PM To: Armstrong, Richard J. Cc: tutor@python.org Subject: Re: [Tutor] Running a dos program with python On Wed, Mar 10, 2010 at 4:51 PM, Armstrong, Richard J. rarms...@water.ca.gov wrote: The dos program pops up and if I hit the enter key three times then it runs. How can I add these three enters into the script? I'm not at all sure if this way would work, but you could send the \r\n through a pipe: p = subprocess.Popen([file1, file2, file3], stdin=subprocess.PIPE) p.communicate(\r\n\r\n\r\n) # Three windows line ending sequences. it also may be possible to add them to the end of the last parameter: 'b.txt\r\n\r\n\r\n' I don't have much faith that it will work, but you can certainly try! HTH, Wayne Wayne thank you so much! This worked beautifully: import subprocess p = subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt'], stdin=subprocess.PIPE) p.communicate(\r\n\r\n\r\n) # Three windows line ending sequences. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a dos program with python
Thanks Randy, maybe I should check out wxPython. From: tutor-bounces+rarmstro=water.ca@python.org [mailto:tutor-bounces+rarmstro=water.ca@python.org] On Behalf Of Randy Raymond Sent: Wednesday, March 10, 2010 3:06 PM To: Tutor Python Subject: Re: [Tutor] Running a dos program with python I use wxPython, which allows a statement like: wx.Execute('c:\shake91.exe FLAC.txt a.txt b.txt') From: Armstrong, Richard J. mailto:rarms...@water.ca.gov Sent: Wednesday, March 10, 2010 4:51 PM To: Wayne Werner mailto:waynejwer...@gmail.com Cc: tutor@python.org Subject: Re: [Tutor] Running a dos program with python From: sri...@gmail.com [mailto:sri...@gmail.com] On Behalf Of Wayne Werner Sent: Wednesday, March 10, 2010 2:24 PM To: Armstrong, Richard J. Cc: tutor@python.org Subject: Re: [Tutor] Running a dos program with python On Wed, Mar 10, 2010 at 3:20 PM, Armstrong, Richard J. rarms...@water.ca.gov wrote: Hello all, This is my first post to the Tutor@python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(rc:\shake91.txt) but cannot do the rest. Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne Wayne, It kindof works. I wrote subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt']) The dos program pops up and if I hit the enter key three times then it runs. How can I add these three enters into the script? Thanks, Richie _ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a dos program with python
Armstrong, Richard J. wrote: Hello all, This is my first post to the Tutor@python.org mailto:Tutor@python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(r”c:\shake91.txt”) but cannot do the rest. Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne Wayne, It kindof works. I wrote subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt']) The dos program pops up and if I hit the enter key three times then it runs. How can I add these three “enters” into the script? Then perhaps something more like this (untested) ... from subprocess import Popen, PIPE proc = Popen([r'c:\shake91.exe'], stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate('FLAC.txt\na.txt\nb.txt\n') If that doesn't work you may need something like 'expect' for windows (or, pexpect and cygwin). HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor