Removing .shp from user output/Changing Name

273
13
Jump to solution
10-09-2018 03:37 PM
AdamThompson3
New Contributor III

So I have created a tool that just mimics the Erase Tool but with a basic license, and the only problem I am running into is when the user selects the output as a shapefile, it messes with my file naming that I do with my processes such as intersect or union. The user is given 3 options to enter, inFc, EraseFc, and Outfc

For example;

ERROR 000732: Selecting Features: Dataset D:\PythonDataTest\ERASE_TESTT.shp_Intersect does not exist or is not supported
Failed to execute (SelectLayerByLocation).

As you can see it is trying to append the _Intersect behind the .shp of the output file name the user selected. And this of course wont work later on when my other processes try and find that shapefile.. SO

my question is how do I get the output name + the string im appending to it, to be before .shp and not after it.

Any help is appreciated thank you!

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

cumbersome, but the key is the split, then the * in the format string.  You could make _intersect a variable if you wanted to

import os
x = os.path.splitext(mypath)
result = "{}_intersect{}".format(*x)
print(result)path/to/some/shapefile_intersect.shp

View solution in original post

13 Replies
DanPatterson_Retired
MVP Esteemed Contributor

make workspace parameter a folder, when gdb's are the workspace, it sort of does that.  When you specify a folder, it can't be a featureclass but a shapefile

0 Kudos
AdamThompson3
New Contributor III

Hey Dan, Good Idea!

Currently what I am doing is making my workspace the same as the output feature class workspace such as this;

WS = os.path.dirname(out_fc)

arcpy.env.workspace = WS

My goal for this tool ideally would be for others to either input a feature class OR shapefile and have it run on either, trying not to limit the user to a specific workspace such as a folder if possible. 

Currently it works fine when the user selects the output as a feature class, just looking to find a way to append that string to the name before the .shp so I  can call on that that XXX_XX_Intersect.shp later on in the script

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

you could simply try getting whatever name it gives you, then replace ".shp" with "" and reappend it to the final file, BUT that would require a folder as output.

If you separate the workspace from the output filename as I suggested, then you would have no issue.  If a gdb is selected as the workspace, then *.shp shouldn't be appended, but you could check and strip it in the code.  If a folder is the destination, then it should have a *.shp on the end, but if it doesn't you could append it in the final code

AdamThompson3
New Contributor III

Hey Dan,

just wanted to thank you again for the help! I found a  bit of a work around to it that works now, it is a bit long and tideous but I got it to work.

Basically what I have done is Performed my Intersect operation at the very beginning with the user inputted features

and then after that I created an IF statement saying if that output file is a shapefile perform this operation;

and then... 

I used the function of [-4] +__NAME in order to get around my .shp naming problem.

But if the output of that first Operation was a feature class then perform the same operations without the [-4] on any of the names and all seems to be good now!

Thanks again.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Glad it worked out Adam

0 Kudos
DarrenWiens2
MVP Honored Contributor

I see you've got a working solution, which is great. I'll just point out the method "os.path.splitext()", which splits out the extension, regardless how long it is. This is handy when you don't exactly know the length of the extension (e.g. dealing with both .jpg and .jpeg):

>>> import os
>>> mypath = 'path/to/some/shapefile.shp'
>>> print(os.path.splitext(mypath))
('path/to/some/shapefile', '.shp')

>>> mypath = 'path/to/some/geodatabase.gdb/featureclass'
>>> print(os.path.splitext(mypath))
('path/to/some/geodatabase.gdb/featureclass', '')‍‍‍‍‍‍‍‍‍
AdamThompson3
New Contributor III

Hey Darren,

I was actually working with that exact function earlier but if the user output was a .shp and I split the text it would remove the .shp and not actually create the shapefile and I couldn't call on it later in the script. Do you know of a way to split the name of a shapefile like you have done above but add string before the .shp? For example

Test.shp --> Test_Intersect.shp

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

cumbersome, but the key is the split, then the * in the format string.  You could make _intersect a variable if you wanted to

import os
x = os.path.splitext(mypath)
result = "{}_intersect{}".format(*x)
print(result)path/to/some/shapefile_intersect.shp
AdamThompson3
New Contributor III

Dan!

This is exactly what I needed to cut my script down in half from what I did earlier for the fix, It is now working for both .shp output and feature class output! Once again really appreciate the help,

Mind explaining to me what that result part does? For example what do the {} mean and I am assuming the *x is format everything in front of the last split part of the .shp? Just wanting to get a better understanding on it. 

0 Kudos