Help running Windows programs from Python

j vickroy jim.vickroy at noaa.gov
Fri May 7 16:38:34 EDT 2010


Scott wrote:
> I want to write a script to automate log archiving/compressing on a
> Win2003 server. I have Python 2.6 installed. I am planning to use 7-
> zip for compression (because I have been using it manually for a while
> now). For now all operations will be local in the C: drive.
> 
> As a total beginner I'm having trouble grasping the control of
> external programs.
> 
> I have found these options but have not tried any yet - I would like
> some advice on which approach would be the most appropriate to begin
> with.
> 
> Command Line Application Approach:
> os.system
> os.spawn*
> os.popen*
> popen2.*
> commands.*
> All of the above recommend replacing with subprocess module
> subprocess - latest way to do command line?
> pexpect module - http://sourceforge.net/projects/pexpect/, Most recent
> file is dated 1/5/2008
>     It should work on any platform that supports the standard Python
> pty  module. (Windows?)
>         pty - "On Windows, only sockets are supported; on Unix, all
> file descriptors." (gulp, ??)
> 
> COM Application Approach:
> Per the 7-zip FAQ: Use the 7z.dll or 7za.dll - I haven't found these
> files yet on sf.net.
> comtypes 0.6.2 - has the word COM in it?
> pywin32 / win32com  - there's that word again.
> 
> And then there is this whole idea of "wrappers."
> 
> Thanks,
> Scott

Hello Scott,

I did not see what version of Python you are using (3.x?, 2.x?) so I'll 
answer based on Python 2.6.4 which I am using.

If you already can accomplish your task with a series of commands, from 
a console window, my recommendation is to first read the Python 
documentation for os.system(...) which allows you to pass, as a 
parameter, any command you can type at a console window.  Then, read the 
subprocess module documentation (particularly Section "18.1.3.3. 
Replacing os.system()").  Use the subprocess module Popen(...) function 
as a replacement for os.system(...).  You may include as many calls to 
Popen(...), in your Python script, as needed.

I do not know much about 7-zip so I can not say if the above 
command-line-like approach is feasible, but a quick glance at the FAQ 
indicates it probably is.

Since you are just getting started, for simplicity, I would recommend 
against trying to use the 7-zip COM 
(http://en.wikipedia.org/wiki/Component_Object_Model) interface or the 
7z.dll (which should be in the C:\Program Files\7-Zip\ folder) directly 
via the Python ctypes module (included in Python 2.6).

HTH,
-- jv



More information about the Python-list mailing list