Executing Hazus Software through Python Script

1202
7
Jump to solution
03-11-2014 10:30 AM
MeredithGreen
New Contributor III
I am trying to write a script that runs Hazus Software but am having a hard time getting started and keep getting an invalid syntax error.  The problem is that I'm not sure if I should also be importing the arcpy module as well since it is linked to ArcGIS or if I should just import os/subprocess.  The other thing that keeps throwing me off is I keep reading numerous solutions to executing a program in general so I don't even know what I should be importing.  Here is what I have right now:

#import os import os os.path.abspath('C:\Program Files (x86)\Hazus-MH\BIN\Hazusp.exe')



I'm still learning a lot when it comes to python so if anybody has an idea how I should start this I would greatly appreciate the input.

Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaChisholm
Occasional Contributor III
Hello Meredith,

You actually pretty close. You were right not to import arcpy. As a good rule of thumb, you should only import a module if you need it. Since you are only using the os module, you should only import the os module.

Also, python reads "\" as a special character (for example "/n" is new line). This means you have to take special actions when inputting a path. There are several ways to do this. One is replacing "/" with "//" or "\" (python reads "\\" or "/" as a "\"). You can also add a 'r' in front of the string to tell python you are inputting a path.

Last thing, if you want the start a program, the command you are likely looking for is os.system().

Try this:
import os os.system(r'"C:\Program Files (x86)\Hazus-MH\BIN\Hazusp.exe"')


Edit: you need double quotes in single quote just for os.system to run the command properly.

Let me know if that was what you were looking for!

View solution in original post

0 Kudos
7 Replies
JoshuaChisholm
Occasional Contributor III
Hello Meredith,

You actually pretty close. You were right not to import arcpy. As a good rule of thumb, you should only import a module if you need it. Since you are only using the os module, you should only import the os module.

Also, python reads "\" as a special character (for example "/n" is new line). This means you have to take special actions when inputting a path. There are several ways to do this. One is replacing "/" with "//" or "\" (python reads "\\" or "/" as a "\"). You can also add a 'r' in front of the string to tell python you are inputting a path.

Last thing, if you want the start a program, the command you are likely looking for is os.system().

Try this:
import os os.system(r'"C:\Program Files (x86)\Hazus-MH\BIN\Hazusp.exe"')


Edit: you need double quotes in single quote just for os.system to run the command properly.

Let me know if that was what you were looking for!
0 Kudos
MeredithGreen
New Contributor III
Thanks for the speedy response.  After making a couple of tweaks this is the new script as of now:

#import os
import os
os.system(r'"C:\Program Files (x86)\Hazus-MH\BIN\Hazusp.exe"')


But I still keep getting an invalid syntax error.
0 Kudos
MeredithGreen
New Contributor III
For some reason the invalid syntax error is highlighting the 6 in Python 2.6.5.  Would anyone know why?

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
    
IDLE 2.6.5      
>>> 
#import os
import os
os.system(r'"C:\Program Files (x86)\Hazus-MH\BIN\Hazusp.exe"')


The Python 2.6.5 at the front is what keeps getting the invalid syntax error.
0 Kudos
MattEiben
Occasional Contributor
This may be a simple fix.  It looks like you have too many quotes in your system command.

Try replacing

os.system(r'"C:\Program Files (x86)\Hazus-MH\BIN\Hazusp.exe"')


with

os.system(r"C:\Program Files (x86)\Hazus-MH\BIN\Hazusp.exe")


All I did there was remove the extra sets of quotes.
0 Kudos
JoshuaChisholm
Occasional Contributor III
Hello Meredith and Matt,

Let me know if Matt's solution works out. I've found that if you are using os.system() and running a file that has spaces in the path you need another set of quotes or windows will try to execute "C:\Program" as a command with the arguments "Files" and "(x86)\Hazus-MH\BIN\Hazusp.exe".

I'm not sure exactly why you're getting a syntax error. We must be missing something simple.

If you running python in command line (one line at a time), make sure you enter only one line at a time (pressing enter to run each line).

Also note that the # character denote a comment. Python will ignore anything on the line after the # character. So #import os is being ignored and import os will actually import the module os.

Sorry if I'm not being clear or if I'm over explaining anything. I'm just trying to make sure we're not missing anything obvious.
0 Kudos
MeredithGreen
New Contributor III
For some reason the invalid syntax error is highlighting the 6 in Python 2.6.5.  Would anyone know why?

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
    
IDLE 2.6.5      
>>> 
#import os
import os
os.system(r'"C:\Program Files (x86)\Hazus-MH\BIN\Hazusp.exe"')


The Python 2.6.5 at the front is what keeps getting the invalid syntax error.


The additional text above the script is automatically included when I open up the python shell to write.  I decided to just delete all of it and it worked!

#import os
import os
os.system(r'"C:\Program Files (x86)\Hazus-MH\BIN\Hazusp.exe"')


Is the correct code and it did open up the hazus program.

Thanks for all of the help!
0 Kudos
JoshuaChisholm
Occasional Contributor III
Oh, sorry, I thought that was you typing into the console, not the actual script itself. Thanks for the update and I'm happy you figured it out!
0 Kudos