Hi GeoNet and thanks in advance. I'm new to Python.
My goal is to write a script that converts all the shapefiles in a gdb into a csv (although I will take an Excel spreadsheet, too). I plan to convert the final program into a script tool with parameters for which gdb to access and where to save the spreadsheets. I can get a single shapefile to convert into an Excel spreadsheet. I would like to loop through the gdb and convert each shapefile to a unique csv (for this case, let's assume that the gdb contains only shapefiles).
My code fails and I have received a couple of different error messages depending on the variable I use for the in_table argument (hope I have that right).
I suspect that I am not correctly naming my in_table and out_xls variables.
Here's a screen shot of my code. Sorry for the small size, not sure how to enlarge the text.
Also I would recommend using the os module for working with filepaths(see os.path.join), its cleaner than using string backslashes and concatenating filenames together.
I agree. Dan Patterson wrote an insightful blog post on the topic.
/blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python
A few things, first this should be a question not a discussion if you are able to fix it,
done...I switched it to a question.
Thanks for the comments, Ian. I the future I will post to questions with questions.
As for the code edits (lines 26/31) I will try this.
I understand the advantage of os.path.join(). I will read the links you shared to learn more on how to use the OS module.
I see your os.path and raise you a pathlib
Don't toy with them Clinton... then can't use 3.6.1 yet let alone 3.anything if they are still in ArcMap
It can still be pip installed for 2.7, its just standard lib in 3.4+ -- but you're right, for base 10.x os.path is the way to go! I'm stuck in the Pro mindset
For those planning ahead, the link in that topic is useful for comparing os and path equivalencies/similarities
https://docs.python.org/3.7/library/pathlib.html#correspondence-to-tools-in-the-os-module
For code formatting, it is best to past it in so it can be retrieved... Code Formatting the Basics ++ if people want to test
You can try this code, taking into account that Excel files are limited to 65,535 (XLS) or 1,048,576 (XLSX) rows:
import arcpy
arcpy.env.workspace = r'D:\MyfolderOrGDB'
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
rows = int(arcpy.GetCount_management(fc).getOutput(0))
if rows < 65535:
desc = arcpy.Describe(fc)
xlsfile = '{}{}{}'.format('D:/OutputFolder/', desc.name, '.xls')
arcpy.TableToExcel_conversion(fc,xlsfile)
Thanks FC
I will try your code. It looks like I might need to make some changes for it to work on my data.
Thanks again