Hello Everybody,
I am trying to read some information from a shape (.shp) file and I am facing little problem when ListFields() function tries to read the file. Here in this code, I am manually passing the file to check.
for field in arcpy.ListFields("L:\\historical_data\\Elko County\\Elko_Sewer\\Elko_Sewer_Network_DBO_ssLateralLine.shp"😞
The above line throws an exception saying, File Doesn’t Exist.
The f = arcpy.ListFeatureClasses() this returns the correct values.
Now the interesting part is, this code runs well in desktop version of the python code. And when I try to run the same code from web version, it returns that above error.Then entire function code is give bellow:
def connectSource(self, filepath):
"""
Connect to a file and create a dictionary data structure that will hold
feature classes:ShapeTypes and field names:Type
"""
self.feature.clear()
listfield = list()
try:
# set workspace to connecting file
arcpy.env.workspace = filepath
# Get list of all features
f = arcpy.ListFeatureClasses()
print(f)
for f1 in f:
listfield[:] = []
i = 0
# Iterate through field list
print(f1)
for field in arcpy.ListFields("L:\\historical_data\\Elko County\\Elko_Sewer\\Elko_Sewer_Network_DBO_ssLateralLine.shp"😞
print('ADC1')
# Filter field
if field.type not in ['OID']: # and field.length < 51:
# Store field name and field type into a list
listfield.insert(i, field.name + " " + field.type)
# Convert list into tuple
hup = tuple(listfield)
i += 1
# Get Shape Type of each Feature Classes
type = arcpy.Describe(f1)
print("t"+type)
# Filter by Shape Type
if type.ShapeType == "Point":
# Store each feature classes as keys and Field name(tuple) as values into a dictionary
self.feature[f1, type.ShapeType] = hup
elif type.ShapeType in ['Polyline', 'Line']:
self.feature[f1, type.ShapeType] = hup
except Exception as e:
print ("Wrong file"+e.message)
tkMessageBox.showerror("Source File Connection", "Failed to connect to the file.{0} ".format(e))
return 0
The same code in the web format throwing an exception that File Doesn’t Exist. Whereas in the desktop version it runs perfectly.
arcpy.ListFields("L:\\historical_data\\Elko County\\Elko_Sewer\\Elko_Sewer_Network_DBO_ssLateralLine.shp")
This ListFields() function aren’t able to return / read the value.
Please guide me where I am wrong.
EDIT 1:
I am trying to develop a python flask application where I want this block of code should get executed from a button click of a HTML file.
So the application is running on http://127.0.0.1:5000/ and once I clicked on that button following function is getting executed.
Following function is being executed on the click of that button:
@app.route('/open_file', methods=['POST'])
def open_file():
proce(os.path.abspath('uploads/Elko_Sewer'))
print('END OPEN')
return render_template('view.html')
In the above code the proce() func has all those code to read from shp file.
Interesting part is, when I call the proce() under the main function of the flask application then it’s really works perfect. And before the debugger gets activate it execute the code and print output in debugger console. I am using PyCharm to develop and debug.
if __name__ == '__main__':
proce(os.path.abspath('uploads/Elko_Sewer'))
app.run(
debug=True)
Regards
Priyam