I am having an issue with raw_input as a variable x. The user types the name and receives returned values. x is in the ly variable is not working.( Line 20) I know that I need to set this up slightly different but not sure how. (Line 22,23)
I can set the variable x as a hardcoded name and I get the correct results which confirms everything else is correct. (Line 21)
import arcpy
import os
#Set folder space
folder = r'S:\\GIS\ServerMXDs\\_Future_Services\\CityWorks'
for filename in os.listdir(folder):
fullpath = os.path.join(folder, filename)
if os.path.isfile(fullpath):
basename, extension = os.path.splitext(fullpath)
if extension.lower() == ".mxd":
mxd = arcpy.mapping.MapDocument(fullpath)
dfs = arcpy.mapping.ListDataFrames(mxd)
for df in dfs:
layers = arcpy.mapping.ListLayers(mxd, "", df)
for layer in layers:
if layer.isFeatureLayer:
lyr_source = layer.dataSource
lyr_name = layer.name.encode("utf8", "replace")
ly = lyr_source.encode("utf8", "replace")-
#x = raw_input("type in feature name: ")
x = 'ServiceEstablishments'
if x in str((ly.split ('\\')[2:])):
print filename + " " + "Service Name:" + " " + df.name + " " + "Layer Name:" + " " + lyr_name
When you say it isn't working with raw_input, what exactly isn't working? Is there an error? If so, what error? Does it run but not print anything?
And, where/how are you running this code?
It runs with a type in feature name: prompt that repeats every time I type something and yield no results.
I am running it in Jupyter Notebook
Not sure if I follow the logic why you are only looking at the slice portion ( ie [2:] ), but I suspect it might have something to do with it....
lyr = r'my\ServiceEstablishments\stuff'
a = 'ServiceEstablishments'
a in lyr
True
a in lyr.split('\\')
True
a in lyr.split('\\')[2:] # should fail the slice is wrong
False
a in lyr.split('\\')[1:] # change the slice... remember slices are 0-based
True
I use the following print statement and get the correct results as seen below (the feature class name)
print "Feature Classes Name:" + str((ly.split ('\\')[2:]))
I have it indexed correctly and results are correct but I have an issue when using the user input.
Feature Classes Name:['BACK.Address_Points']
Well it returns a string, but is that a little minus sign on the line before your commented out line?
I use python 3.X so raw_input is now 'input', just to show that it does return a string
b = input("type in feature name: ")
type in feature name: back.atyoub
'back.atyou'
c = 'here is a string... back.atyou'
b in c
True
Thanks for this advice.
I haven't used the in very much, I did and it works.
I now see that the
if x in str((ly.split ('\\')[2:])):
is causing the issue.
It is indexed correctly, but something is not right in the loop input syntax.
Just a couple of things that might help in my understanding your question. First, escaping vs. raw string, I just want to make sure this isn't the actual issue.
folder = r'S:\\GIS\ServerMXDs\\_Future_Services\\CityWorks'
# should be
folder = r'S:\GIS\ServerMXDs\_Future_Services\CityWorks'
# or
folder = 'S:\\GIS\\ServerMXDs\\_Future_Services\\CityWorks'
Second. Are you examining the layer source path for a specific name, such as layer.dataSource contains "ServiceEstablishments"? Are you also checking layer.name for this value as "ServiceEstablishments" may be an alias for a data source path that does not contain the phrase?
I think your "in" and "split" would otherwise work.
sort of agree, but at the operating system level...
a = r'C:\Temp\a\aa\a0\a00'
os.path.exists(a)
True
b = r'C:\\Temp\\a\\aa\\a0\\a00'
os.path.exists(b)
True
I tried the different \\ regarding escape characters and I don't get any different results.
I am looking up layer.dataSource which is the actual feature class name.