Select to view content in your preferred language

Arcpy.ListWorkspaces returns wrong results

907
2
Jump to solution
11-22-2017 11:54 AM
AhmadSALEH1
Frequent Contributor

Hi, 

I am trying to get a list of database connections stored in the folder, the script runs successfully but it gives wrong results!

For example,  "inFolder=r'C:\1\test'"  has only one database connection which is u'C:\\1\\test\\Urbab.sde', but when I run the script it displays it 9 times every time! I tried to run the script from the python window in the ArcMap and I also used PyScripter and both of them gives the same result.

import arcpy, sys, os

inFolder=r'C:\1\test'
outFolder=r'C:\1\test2'
print "Connecting to geodatabase"
arcpy.env.workspace=inFolder
arcpy.env.overwriteOutput=True

for DBConn in inFolder:
       x=arcpy.ListWorkspaces("*","SDE")
       print x

What might be the issue here?

Thanks,

Ahmad

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Emeritus

The reason it is displaying 9 times, it is most likely because your

   for DBConn in inFolder

loops thru each character of the inFolder path.

That is, if you replace the print X with print DBConn you would get (this is why you are printing the same X 9 times)

C
:
\
1
\
t
e
s
t

So, you will want to populate "x" prior to the loop, then do something like

for DBConn in x:

  print DBConn

View solution in original post

2 Replies
RebeccaStrauch__GISP
MVP Emeritus

The reason it is displaying 9 times, it is most likely because your

   for DBConn in inFolder

loops thru each character of the inFolder path.

That is, if you replace the print X with print DBConn you would get (this is why you are printing the same X 9 times)

C
:
\
1
\
t
e
s
t

So, you will want to populate "x" prior to the loop, then do something like

for DBConn in x:

  print DBConn

AhmadSALEH1
Frequent Contributor

Thanks Rebecca, This was helpful.

Here below how the script looks now. 

import arcpy, sys, os

inFolder=r'C:\1\test'
outFolder=r'C:\1\test2'
print "Connecting to geodatabase"
arcpy.env.workspace=inFolder
arcpy.env.overwriteOutput=True

Workspace=arcpy.ListWorkspaces("*","SDE")
for DBConn in Workspace:
       print DBConn