How can I create a arcpy script/tool to open a file?

4541
7
Jump to solution
11-25-2015 12:26 PM
RebeccaStrauch__GISP
MVP Emeritus

In my custom toolbar, I have series of steps that need to be processed in order. Between two of these steps, it is necessary to do quite a bit of manual editing (in ArcMap...polygons)....based on some input from staff, and most likely from some paper sources.  I will have a document of some type (.doc, .txt or similar) will some detailed instructions.

Is there a way that I can open a document thru a python/arcpy script and add it to my toolbar?  If I can get the script, I should be able to do the toolbar/addin part, but I haven't figure out how to "open" a readme type file thru a toolbox script/button.

In theory, I would probably not even have any input parameters, and I would hard code the (relative) path to the document.  I've started searching for solutions, but thought I would throw this question out there in case a solution already exists.  I have not written any specific code for this problem yet (i.e., nothing worth sharing).

If I can figure this out, I will use this in several of my addin projects.

Thanks

cross-posting:  DevelopersGeoprocessingPython AddInspython snippets

0 Kudos
1 Solution
7 Replies
RebeccaStrauch__GISP
MVP Emeritus

Thanks Ian.  Quite a bit to look thru and test (first simple test didn't).  I'll let you know how it works.

0 Kudos
IanMurray
Frequent Contributor

If using windows, this is probably the easiest solution.  Some people on stackexchange get a little overly pythonic.

15.1. os — Miscellaneous operating system interfaces — Python 2.7.11rc1 documentation

RebeccaStrauch__GISP
MVP Emeritus

Actually, I got it to work in the python window, if I hard code the path.  I now have the script created, and it works with my relative path.   My .txt file is in the same folder as all my scripts.

Thanks for the quick response Ian.  For some reason I just couldn't come up with a good search string.

Oh...and for those that care, I added the script to my toolbox....no parameters for right now and it opens my txt file.

import os
import arcpy


#filename = r"C:\___bear2016\__python\Scripts\3MyTestHelp.txt"
filename = r"3MyTestHelp.txt"
os.system("start " + filename)

I know I don't need to import arcpy right now.....but haven't removed it yet since I may add more stuff.

0 Kudos
IanMurray
Frequent Contributor

yea it took a few tries for me to find the right search.  Plenty of threads on how to open files within python, but not a bunch on launching them externally.

curtvprice
MVP Esteemed Contributor

os.system() is the old way to do it, though still okay for the "quick and dirty"

I'm trying to do this the new way, which is to use subprocess.

The new way is more secure, as it doesn't have as much risk of code injection.

import subprocess
subprocess.check_output(["cmd", "/c", "start", filename])

The windows-only os.startfile() is also mentioned in that (excellent) stack exchange article. I didn't know about that one -- it emulates double clicking in Windows Explorer. It has the advantage of supporting files with spaces!

os.startfile(filename)
RebeccaStrauch__GISP
MVP Emeritus

Thanks Curtis.  I'll take a look at that too. Just trying to keep it all simple for the "pop up"

0 Kudos