Arcpy copy feature out of path in a table?

2830
11
Jump to solution
04-11-2016 05:22 AM
JohannesBierer
Occasional Contributor III

I've a table with paths like:

R:\Daten\geo_daten\GEOLOGIE\bodenuebersicht\daten\arcview\bodeneigenschaften\shape\bodeneigenschaften.shp

and want to copy this Feature somewhere else and delete in the location where it is?

What's wrong?

import arcpy, numpy
arcpy.env.overwriteOutput = True
InPath = r"R:\Karto\Geodaten_Archiv.txt"
OutPath = r"R:\Daten\geo_daten\geodaten_archiv\SIC_2016_04_07"
filename = open(InPath, 'r')
arr = arcpy.da.TableToNumPyArray(InPath, "Datenpfad")
for line in arr:
    print line
    arcpy.CopyFeatures_management(line, OutPath)
0 Kudos
1 Solution

Accepted Solutions
WesMiller
Regular Contributor III

Forgot it was an array, see if this works

import arcpy, numpy ,os   
arcpy.env.overwriteOutput = True    
InPath = r"F:\Departments\GIS\Wes\PySripts\DanPatterson\Code4GeoNet\CopyTest\Paths.txt"    
OutPath = r"F:\Departments\GIS\Wes\PySripts\DanPatterson\Code4GeoNet\CopyTest\CopyTo"  
filename = open(InPath, 'r')  #Not being used  
arr = arcpy.da.TableToNumPyArray(InPath, "Datenpfad")    
for line in arr:    
    print line[0]
    print os.path.join(OutPath,os.path.basename(line))
    arcpy.CopyFeatures_management(line[0], os.path.join(OutPath,os.path.basename(line[0]))) 

View solution in original post

11 Replies
WesMiller
Regular Contributor III

When you use print line in 8 what do you get. ON line 9 you are coping whatever is printed on line 8 but you are printing to the same name over and over. I didn't test this but you may need to do something similar to below


import arcpy, numpy ,os 
arcpy.env.overwriteOutput = True  
InPath = r"R:\Karto\Geodaten_Archiv.txt"  
OutPath = r"R:\Daten\geo_daten\geodaten_archiv\SIC_2016_04_07"  
filename = open(InPath, 'r')  #Not being used
arr = arcpy.da.TableToNumPyArray(InPath, "Datenpfad")  
for line in arr:  
    print line  
    arcpy.CopyFeatures_management(line, os.path.join(OutPath,os.path.basename(line)))
0 Kudos
JohannesBierer
Occasional Contributor III

Thank you for your comment, but your idea leads to this:

(u'R:\\Daten\\geo_daten\\GEOLOGIE\\bodenuebersicht\\daten\\arcview\\bodeneigenschaften\\shape\\bodeneigenschaften.shp',)

Traceback (most recent call last):
 
    arcpy.CopyFeatures_management(line, os.path.join(OutPath,os.path.basename(line)))
  File "C:\Python27\ArcGIS10.2\lib\ntpath.py", line 198, in basename
    return split(p)[1]
  File "C:\Python27\ArcGIS10.2\lib\ntpath.py", line 170, in split
    d, p = splitdrive(p)
  File "C:\Python27\ArcGIS10.2\lib\ntpath.py", line 125, in splitdrive
    if p[1:2] == ':':
IndexError: invalid index

0 Kudos
WesMiller
Regular Contributor III

What prints on line 8?

0 Kudos
NeilAyres
MVP Alum

You originally said this ( see my comments in red😞 "

I've a table with paths like:

R:\Daten\geo_daten\GEOLOGIE\bodenuebersicht\daten\arcview\bodeneigenschaften\shape\bodeneigenschaften.shp

and want to copy this Feature somewhere else and delete in the location where it is?

What's wrong?

  1. import arcpy, numpy 
  2. arcpy.env.overwriteOutput = True 
  3. InPath = r"R:\Karto\Geodaten_Archiv.txt"  # This isn't a path, it is a file
  4. OutPath = r"R:\Daten\geo_daten\geodaten_archiv\SIC_2016_04_07" 
  5. filename = open(InPath, 'r')  # not using this
  6. arr = arcpy.da.TableToNumPyArray(InPath, "Datenpfad")  # why are you converting to a numpy array ???
  7. for line in arr: 
  8.     print line 
  9.     arcpy.CopyFeatures_management(line, OutPath) # there is no file specified

Here you point to a shapefile : R:\Daten\geo_daten\GEOLOGIE\bodenuebersicht\daten\arcview\bodeneigenschaften\shape\bodeneigenschaften.shp

Then you read from a text file in your code???

If you just want to copy a bunch of shapefiles from one location to the other, you could do something like this :

import sys, os, arcpy
inPath = "some path here"
outPath = "some other path here"
arcpy.env.workspace = inPath
shpIn = [for f in arcpy.ListFeatureClasses() if f.endswith(".shp")]

for shp in shpIn:
    arcpy.CopyFeatures_management(shp, os.path.join(outPath, shp))

All this, more or less from :

Copy Features—Help | ArcGIS for Desktop

0 Kudos
WesMiller
Regular Contributor III

Forgot it was an array, see if this works

import arcpy, numpy ,os   
arcpy.env.overwriteOutput = True    
InPath = r"F:\Departments\GIS\Wes\PySripts\DanPatterson\Code4GeoNet\CopyTest\Paths.txt"    
OutPath = r"F:\Departments\GIS\Wes\PySripts\DanPatterson\Code4GeoNet\CopyTest\CopyTo"  
filename = open(InPath, 'r')  #Not being used  
arr = arcpy.da.TableToNumPyArray(InPath, "Datenpfad")    
for line in arr:    
    print line[0]
    print os.path.join(OutPath,os.path.basename(line))
    arcpy.CopyFeatures_management(line[0], os.path.join(OutPath,os.path.basename(line[0]))) 
WesMiller
Regular Contributor III

If that works here is the Delete—Help | ArcGIS for Desktop  help

0 Kudos
JohannesBierer
Occasional Contributor III

line[0] was it, thanks a lot

0 Kudos
JohannesBierer
Occasional Contributor III

Would like to mark your answer as the correct one, but there's only helpful and like buttons. Sorry wasn't here for a long time?

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Johannes,

See if you can still change this from a "discussion" to a "question", then you should be able to check a correct answer.  Timothy Hales​ can probably help if you aren't able to do this.