Basics - Using a geodatabase feature class

2023
8
Jump to solution
01-16-2016 04:02 PM
JúlioLuta
New Contributor III

Hi,

It's the first time using a Python script - so far I just used some statements in the field calculator - and my coding background is small and distant.

I'm using and fiddling a script I saw in another forums but I always get "Parsing error SyntaxError: invalid syntax (line x)" when I add the files I'll use in the script.

I've tried all I read in the tutorials I did and things I googled:

points = “D:\\OneDrive\\Documents\\Tese\\TrabalhoPratico\\Final\\01.gdb\\Lisbon_3D\\Points10”

points = “D:/OneDrive/Documents/Tese/TrabalhoPratico/Final/01.gdb/Lisbon_3D/Points10”

points = open(“D:/OneDrive/Documents/Tese/TrabalhoPratico/Final/01.gdb/Lisbon_3D/Points10”, "w")

I know this is probably really simple... but I'm not getting it with my (it seems) limited google fu.

Cheers

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

One thing that will not work is using the following line:

points = open(“D:/OneDrive/Documents/Tese/TrabalhoPratico/Final/01.gdb/Lisbon_3D/Points10”, "w")

That's because:

  • you are using a syntax to write to a text file when this is a featureclass "Points10" inside a Feature Dataset "Lisbon_3D" inside a file geodatabase called "01.gdb"
  • your quotes around the "output file" may have been copied from Word or the Internet but aren't valid quotes like then ones around the "w" at the end of the line.

A very important resource to learn about writing information to a featureclass is: Accessing data using cursors—Help | ArcGIS for Desktop . You will need the InsertCursor to write new features or the UpdateCursor to update existing features.

And as Dan is suggesting, please share more of the script you have tried, since that will help us to find out what you are doing wrong.

View solution in original post

8 Replies
DanPatterson_Retired
MVP Esteemed Contributor

you will have to post you script that is trying to load the layers

0 Kudos
XanderBakker
Esri Esteemed Contributor

One thing that will not work is using the following line:

points = open(“D:/OneDrive/Documents/Tese/TrabalhoPratico/Final/01.gdb/Lisbon_3D/Points10”, "w")

That's because:

  • you are using a syntax to write to a text file when this is a featureclass "Points10" inside a Feature Dataset "Lisbon_3D" inside a file geodatabase called "01.gdb"
  • your quotes around the "output file" may have been copied from Word or the Internet but aren't valid quotes like then ones around the "w" at the end of the line.

A very important resource to learn about writing information to a featureclass is: Accessing data using cursors—Help | ArcGIS for Desktop . You will need the InsertCursor to write new features or the UpdateCursor to update existing features.

And as Dan is suggesting, please share more of the script you have tried, since that will help us to find out what you are doing wrong.

View solution in original post

JúlioLuta
New Contributor III

It was the quotes. *loud doh* I've corrected the ones I had copied from the internet with proper ones.

Thanks for the explanation about text file vs geodatabase.

Thanks to both for the replies!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Glad the comment helped you. I didn't think that was going to be the issue, just wanted to mention it.

I'm sure you are not using the third option to access the data since that would generate the following error:

IOError: [Errno 2] No such file or directory: 'C:\\GeoNet\\writeFeatures\\01.gdb\\Lisbon_3D\\Points10'

Ignore the first part of the path, since it refers to the location on my drive. The error in this case is caused since the folder "Lisbon_3D" does not exist and therefor the text file Points10 cannot be created. The Feature Dataset is not stored as a physical folder in the geodatabase "folder".

There is another tip I would like to share. You showed two ways to store the path to the data:

  • double backslashes
  • single forward slashes

There is a third way I normally use, which may be faster and that is the raw ("r") notation. If you open the properties of a layer in ArcMap and go to the Source tab, you can copy the path to the data:

I you past it and correct it to "C:\GeoNet\writeFeatures\01.gdb\Lisbon_3D\Points10" you will only have to place an "r" before it to make it a valid path in Python (see line 3). Your options are:

points = "D:\\OneDrive\\Documents\\Tese\\TrabalhoPratico\\Final\\01.gdb\\Lisbon_3D\\Points10"

points = "D:/OneDrive/Documents/Tese/TrabalhoPratico/Final/01.gdb/Lisbon_3D/Points10"

points = r"D:\OneDrive\Documents\Tese\TrabalhoPratico\Final\01.gdb\Lisbon_3D\Points10"

This might save you some time.

JúlioLuta
New Contributor III

Thanks for the tip.

I've been using the location toolbar in ArcMap to copy and paste. Then adding backslashes. Just adding the r will be quite faster for sure.

Yes I'm using the first option with the corrected quotes. Next time I'll be using the r one.

DanPatterson_Retired
MVP Esteemed Contributor

Grief... your eyes are better than mine...

>>> points = “yup_wrong_quotes_fail”

  File "<string>", line 1

    points = “yup_wrong_quotes_fail”

            ^

SyntaxError: invalid syntax

WesMiller
Regular Contributor III

To expand on an easy way to get a feature class path. See below.

JúlioLuta
New Contributor III

My second *doh* in this thread. I had look before to the location there and I was "hmm it just shows the name, what a shame", I never remembered to click on it.

Thanks!

0 Kudos