import arcpy import os fc = r" C:\Users\GIS\Documents\TEST 2-25\project_test.shp" fields = ["PROJECT_DW"] 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:\Users\GIS\Documents\brokenlinks.txt","a") f.write(Roll + os.linesep) f.close()
Hi,
I adapted some code I found online to search for broken (moved or renamed) hyperlinks in a shapefile. It should write all broken links to a text file, but instead it is writing ALL the hyperlinks to the text file. Any advice on what to change? I know extremely little about python.
Also, I have arcmap 10.1
thank you!import arcpy import os fc = r" C:\Users\GIS\Documents\TEST 2-25\project_test.shp" fields = ["PROJECT_DW"] 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:\Users\GIS\Documents\brokenlinks.txt","a") f.write(Roll + os.linesep) f.close()
Roll = str(row[0]) Roll = Roll.strip()
import arcpy import os from urllib2 import urlopen fc = r" C:\Users\GIS\Documents\TEST 2-25\project_test.shp" fields = ["PROJECT_DW"] with arcpy.da.SearchCursor (fc,fields) as cursor: for row in cursor: Roll = str(row[0]) try: urlopen(Roll) except: f = open(r"C:\Users\GIS\Documents\brokenlinks.txt","a") f.write(Roll + os.linesep) f.close()
import arcpy import os fc = r" C:\Users\GIS\Documents\TEST 2-25\project_test.shp" fields = ["PROJECT_DW"] with arcpy.da.SearchCursor (fc,fields) as cursor: for row in cursor: Roll = str(row[0]) ## print out the link to verify it #try to print it like this first: print "{0}".format(row[0]) #try to print it like this too: print str(Roll) if os.path.exists(Roll): pass else: f = open(r"C:\Users\GIS\Documents\brokenlinks.txt","a") f.write(Roll + os.linesep) f.close()
Sure, here are examples of types of hyperlinks we will search through:
folders: F:\LP04\COPP DUNN PLANTATION
pdfs: F:\MSL\EMAIL SURVEYS\STANDARD PACIFIC\DUNN PLANTATION\L65 GBBF 10-30-03.PDF
dwgs: F:\LP04\COPP DUNN PLANTATION\DWG\116-03-295 LT65.DWG
If spaces are an issue, than I'm pretty much screwed. There is way too much data to change all the names..
Tried the print function- again displayed ALL the hyperlinks instead of just the broken ones (I entered some fake links for testing).
roll = 'H:\exists\broken\dfBA D.csv' if os.path.exists(roll): print "roll exists: " + roll else: print "roll failed"
roll = r'H:\exists\broken\dfBA D.csv' if os.path.exists(roll): print "roll exists: " + roll else: print "roll failed"
import arcpy import os fc = r" C:\Users\GIS\Documents\TEST 2-25\project_test.shp" fields = ["PROJECT_DW"] with arcpy.da.SearchCursor (fc,fields) as cursor: for row in cursor: Roll = str(row[0]) Roll = Roll.replace("\\", "\\\\") if os.path.exists(Roll): pass else: f = open(r"C:\Users\GIS\Documents\brokenlinks.txt","a") f.write(Roll + os.linesep) f.close()
roll = roll.replace('\\', r'/')