File Doesn't Exist Exception on arcpy.ListFields()

745
6
05-19-2018 03:19 AM
PriyamPaul
New Contributor

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

Reply
0 Kudos
6 Replies
JoshuaBixby
MVP Esteemed Contributor

Can you explain "web format" a bit more, i don't quite understand what you mean.  Are you trying to run it as a geoprocessing task on ArcGIS Server?  If that is the case, it raises the question of whether the process running ArcGIS Server has the proper configuration and credentials to access the path of the shapefile.  The error might be "file does not exist" but that sometimes means it just doesn't exist to the process trying to access it, not that is doesn't exist at all.

Reply
0 Kudos
PriyamPaul
New Contributor

Hello, To your question I am explaning the "web format" a bit.

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)

Reply
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Can you post the error message with traceback included?  You say that arcpy.ListFields is generating the error, but it would be helpful to have the traceback and entire error message.

Reply
0 Kudos
EsadKopru1
New Contributor II

Hello Joshua, 

I developed a Flask-Python application that uses both arcpy and pymssql libraries in it. When I run the app on the console, everything works, however, when I do it on the Flask app on 127.0.0.1:5000,  the pymssql methods work but the arcpy methods don't. I tried Django for the same thing but had the same problem. The table is created but the arcpy method is not able to find it, below is the traceback,

Additionally, there is the question about the issue on StackOverflow, Python Flask/Django app with ArcGIS, Arcpy does not run on Flask or Django - Stack Overflow 

Any help would be appreciated.

Thanks,

Esad

ERROR "F:\Projects\Dashboard\Publish.sde\Publish.dbo.A_WebFlask" does not exist
Traceback (most recent call last):
File "C:\Flask_Dash\DashboardWithFlask.py", line 163, in my_form
myLayer.dashboardMain()
File "C:\Flask_Dash\DashboardClass.py", line 623, in dashboardMain
descTable = arcpy.Describe(self.dbTablePath)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\__init__.py", line 1253, in Describe
return gp.describe(value)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\geoprocessing\_base.py", line 376, in describe
self._gp.Describe(*gp_fixargs(args, True)))
IOError: "F:\Projects\Dashboard\Publish.sde\Publish.dbo.A_WebFlask" does not exist

Reply
0 Kudos
JoeRogan
New Contributor III

Hi, I am wondering if a solution was found to this?  I am also trying to run arcpy in a python Flask app and having trouble accessing files within the scope of the Flask app.  To make it super simple I am just trying to run arcpy.Exists() on a table in a gdb on my local drive.  When the line of code is before the Flask app it will return True, but within the Flask app it returns False.  If I use os.path.exists, this returns True when it should, the problem is always with arcpy. Thanks!

Reply
0 Kudos
EsadKopru1
New Contributor II

No solutions yet. Nothing in the StackOverflow either.  

Reply
0 Kudos