Select to view content in your preferred language

Launching .exe in python

57631
19
12-28-2010 11:30 AM
DanPajak
Regular Contributor
I am trying to launch microsoft access.exe and have know errors but also no result with opening access. Does anyone know how to do this. I am using ArcDesktop ten with latest service pack and have python 2.6 installed. 


import os
os.spawn("c:\programs files\Microsoft office\office12\msaccess.exe")
Tags (2)
0 Kudos
19 Replies
DanPajak
Regular Contributor
Thanks for you help I finally was able to launch the access from python.  I realized that I couldn't just copy and past code.  It only worked when I typed it out.  Now that I get access to open is there a way to launch a macro from python.  I tried
>>> import subprocess
>>> subprocess.Popen([r"C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE" "\\vopcc\gis\GISOPS\StreetNetwork\Routing.mdb" /x WSorderMacro])
SyntaxError: invalid syntax
>>> 
  and also tried
>>> import subprocess
>>> subprocess.Popen([r"C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE" "\\vopcc\gis\GISOPS\StreetNetwork\Routing.mdb""WSorderMacro"])

Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    subprocess.Popen([r"C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE" "\\vopcc\gis\GISOPS\StreetNetwork\Routing.mdb""WSorderMacro"])
  File "C:\Python26\ArcGIS10.0\lib\subprocess.py", line 633, in __init__
    errread, errwrite)
  File "C:\Python26\ArcGIS10.0\lib\subprocess.py", line 842, in _execute_child
    startupinfo)
WindowsError: [Error 3] The system cannot find the path specified
and get errors.  Thanks again for all your help so far it has been much aprreciated:)
0 Kudos
V_StuartFoote
MVP Frequent Contributor
Jeremy,

Others may have a better feel for it, but it seems a little ambitious to use UNC ref to the \\vopcc\gis mount while working out syntax and function.

To simplify debugging,  I would copy or subset the Access MDB to the local file system along with the macro, or at the least Windows map the share.

Finally, I think that the args provided to the Popen need to be separated by commas, but still raw. So wouldn't that be:
subprocess.Popen([r"C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE", r"\\vopcc\gis\GISOPS\StreetNetwork\Routing.mdb", r"/x WSorderMacro"])


Stuart
0 Kudos
LoganPugh
Frequent Contributor
Based on the Python help on subprocess, you can (and on Windows, probably should) use a string instead of a list of arguments to specify the command line. Access seems particularly picky about how you specify its command line. This should work:

import subprocess
subprocess.Popen(r'"C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE" "\\vopcc\gis\GISOPS\StreetNetwork\Routing.mdb" /X WSorderMacro')


Note the raw string indicator, single quotes around the entire string, and the double quotes around paths (which are required if the path contains spaces, but it's good to include them anyways).
0 Kudos
ChrisMathers
Deactivated User
This is how I open excel and it works fine. I use a list of arguments separated by a comma. Both are rawed strings.

subprocess.Popen([r'C:\Program Files\Microsoft Office\OFFICE11\EXCEL.exe',r'C:\GIS Projects\TaxChange\Export\Change.xls'])
0 Kudos
DanPajak
Regular Contributor
I have finally got it to open and run the macro.  Thanks again for all your help. 
>>> import subprocess
>>> subprocess.Popen([r'C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE',r'\\vopcc\gis\GISOPS\StreetNetwork\Routing.mdb'])
I am having one small issue when I save the file as a .py file type and then double click on the file nothing happens.  How do I make this script into a .py file and run the script off of that.
0 Kudos
JasonScheirer
Esri Alum
Are the literal >>>s in your script? If so, that's your problem.
0 Kudos
DanPajak
Regular Contributor
Thank You very much.  I removed the >>> and am now able to launch this from a .py.   Thank you all for your help.  Had to redu a whole work process because of ArcMap 10 going away with VB.  Hopefully creating all the buttons in Python I will not have to change this again.
0 Kudos
JohnHawkins
Emerging Contributor
I also had this problem but didn't see the answer in where vopgis said he got it to work so I am including a script that I got to work. Note: the popen runs the process in parallel and what I wanted was to finished a process then move on.  if you use the subprocess.call(program)  it will finish the msaccess call then continue.


import subprocess
def MacroAccess(when):
    if when == 'Nightly': #either Nightly or Weekly
        program = r'"C:\Program Files (x86)\Microsoft Office\OFFICE11\msaccess.exe" "c:\ftp\import2.mdb " /x NightlyBatch'
    elif when == 'Weekly':
        program = r'"C:\Program Files (x86)\Microsoft Office\OFFICE11\msaccess.exe" "c:\ftp\import2.mdb " /x WeeklyBatch'
    else:
        return 'no'
    print when + 'batch'
    subprocess.call(program)
    return when + 'Batch has run'
    print 'done'

print MacroAccess('Nightly')
0 Kudos
LukeSturtevant
Frequent Contributor

I know this is an old thread but this seems to work for me:

import os 

filepath = r"C:\project_front.accdb"
os.system("start MSACCESS.EXE {}".format(filepath ))

Thanks Wes, I just updated.

0 Kudos
WesMiller
Deactivated User

Luke shouldn't line 4 be

os.system("start MSACCESS.EXE {}".format(filepath))

0 Kudos