Help running Windows programs from Python

S

Scott

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
 
M

Mensanator

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

Here's a simple example:

Volume in drive C has no label.
Volume Serial Number is 06DE-55B4

Directory of c:\python31\user

09/08/2009 01:38 PM 689 3435.py
11/10/2009 06:43 PM 614 4697.py
03/09/2010 08:42 PM 5,922 5x.py
03/30/2010 07:51 PM 1,937 7[18] impure.py
08/28/2009 08:43 PM 1,867 bones.py
05/05/2010 07:21 PM 441 code.py
03/22/2010 04:31 PM 639 collatz_curiosity.py
09/18/2009 08:06 PM 17,125 collatz_game.py
08/20/2009 03:09 PM 406 csv_file_fix__minimal_2_all.py
11/06/2009 08:18 PM 432 digitsets.py
07/20/2009 07:03 PM 14,451 gmpy_geni_test_3.py
10/23/2009 05:35 PM 2,952 hit_target.py
03/19/2010 07:52 PM 245 how_many_random.py
04/15/2010 07:06 PM 217 issquare.py
07/31/2009 05:14 PM 1,132 itune.py
07/31/2009 04:41 PM 527 itune_stats.py
10/08/2009 08:08 PM 6,202 make_numbers.py
04/14/2010 08:00 PM 354 montyhall4.py
04/21/2010 07:46 PM 1,196 Ryan_Whited.py
03/25/2010 07:53 PM 4,318 ultimate_cycle.py
20 File(s) 61,666 bytes
0 Dir(s) 102,634,008,576 bytes free
 
J

j vickroy

Scott said:
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
 
J

John Yeung

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.

For your purposes, I personally think os.system() is enough of an
interface to 7-Zip and any other basic operating system commands you
might need for your task. It's what I use myself to do batch
archiving with 7-Zip.

I think you will find everything you need within the os module (to get
the names of files and such), and of course Python's built-in string
methods.

Especially since you say you are a beginner, my recommendation is to
keep it simple; and to me, using only the os module is the simplest
way to go. (That is why I went with it, after all. ;)

Also, don't be afraid to try stuff. I can understand not wanting to
mess stuff up. Well, create your own test directory and dummy files
to practice on first, and just have at it.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top