3

I've recently come across an issue where some of our hyperlinks aren't working. I'm familiar with python and have made similar scripts before, but I would first like to ask for your assistance to maybe something already pre-existing or another tool to use.

I would like to cycle through all the hyperlink paths for a certain shapefile and search the C:\Scans\ for the path. If the path doesn't exist than I want the path to be written to an excel file.

Can anyone help me with this?

Final code so far

import arcpy
import os
fc = r"G:\GIS\Lauren\Shapes\watermains.shp"
fields = ["Truck_Path"]
with arcpy.da.SearchCursor (fc,fields) as cursor:
 for row in cursor:
 Roll = str(row[0])
 if os.path.exists(Roll):
 pass
 else:
 f = open(r"C:\Scans\BrokenLinks.txt","a")
 f.write(Roll + os.linesep)
 f.close()
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 8, 2013 at 16:02
1
  • 1
    Just as a general comment, I would leave your original question as you posted it, with the code you were having trouble with. That way, if someone looks at it, and then at the answer, they will be able to identify the difference. I saw your comment and added the line separator into my code as well. Good catch on that, sorry I missed it to start with. Commented Nov 8, 2013 at 19:24

3 Answers 3

6

I made just a couple of modifications to your script.

  1. I fixed your fields variable so that it would not throw an error. Previously, you did not have it wrapped as a string, enclosing with single quotes fixes that problem. This also allows you to expand the script to include additional fields if need be, with a minimum of fuss. It also lets you get rid of the additional variable you had to enter the field name for "Truck_Path".

  2. Where you are testing to see if the string exists, I changed the reference there to be the variable Roll, which is what you assigned the file path attribute to. This way, it is searching to see if the file path in that variable exists, as opposed to Truck_Path which is what you had before, which would have simply searched for the text, "Truck_Path".

  3. I added simple code to open an existing text file, write the file path into it if there was no match, and close it again. This file would be easy enough to open in Excel for further modification, but doesn't require bringing in a specific module for handling Excel.

  4. Removing a number of the Import references that you have. Unless you plan to use them later, no sense referencing them because I think Python tries to verify them whether they are used or not, thus causing slight overhead on the script. I'm not completely sure about that, but the simpler the better.

Here is the updated code:

import arcpy
import os
fc = r"G:\GIS\water\watermains.shp"
fields = '["Truck_Path"]'
with arcpy.da.SearchCursor (fc,fields) as cursor:
 for row in cursor:
 Roll = str(row[0])
 if os.path.exists(Roll):
 pass
 else:
 logfile = open(r"c:\filepath.txt","a")
 logtext = Roll + os.linesep
 logfile.write(logtext)
 logfile.close()
answered Nov 8, 2013 at 18:16
3
  • Thanks! I realized my error after I posted my code... lol but yours is definitely neater! Commented Nov 8, 2013 at 18:21
  • 1
    Is there any way to make each new entry appear on a new line? Commented Nov 8, 2013 at 18:51
  • I've updated my post to what I ended up using Commented Nov 8, 2013 at 19:13
1

In order to write each broken link to a new line I adapted @GetSpatial's code to this:

import arcpy
import os
fc = r"G:\GIS\Lauren\Shapes\watermains.shp"
fields = ["Truck_Path"]
with arcpy.da.SearchCursor (fc,fields) as cursor:
 for row in cursor:
 Roll = str(row[0])
 if os.path.exists(Roll):
 pass
 else:
 f = open(r"C:\Scans\BrokenLinks.txt","a")
 f.write(Roll + os.linesep)
 f.close()
answered Nov 8, 2013 at 19:15
0

Using a list comprehension with embedded logic is an efficient approach to this problem. Using arcpy.Exists() function also allows you to check for the existence of feature classes in a geodatabase (note that os.path.exists() will not work for this).

import arcpy
fc = r'C:\path\to\fgdb.gdb\some_fc'
li = [row[0] for row in arcpy.da.SearchCursor(fc, ["Truck_Path"]) if not arcpy.Exists(str(row[0]))]
for l in li:
 if l != None: # Handle None type values
 f = open(r"C:\temp\BrokenLinks.txt","a")
 f.write(l + os.linesep)
 f.close()
answered Jul 18, 2015 at 11:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.