Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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? It would be greatly appreciated, thanks!

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()
share|improve this question
    
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. –  Get Spatial Nov 8 '13 at 19:24

2 Answers 2

up vote 4 down vote accepted

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()
share|improve this answer
    
Thanks! I realized my error after I posted my code... lol but yours is definitely neater! –  GISKid Nov 8 '13 at 18:21
1  
Is there any way to make each new entry appear on a new line? –  GISKid Nov 8 '13 at 18:51
    
I've updated my post to what I ended up using –  GISKid Nov 8 '13 at 19:13

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()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.