6

I am trying to copy only certain files from one folder to another. The filenames are in a attribute table of a shapefile.

I am successful upto writing the filenames into a .csv file and list the column containing the list of the filenames to be transferred. I am stuck after that on how to read those filenames to copy them to another folder. I have read about using Shutil.copy/move but not sure how to use it. Any help is appreciated. Below is my script:


import arcpy
import csv
import os
import sys
import os.path
import shutil
from collections import defaultdict
fc = 'C:\\work_Data\\Export_Output.shp'
CSVFile = 'C:\\wokk_Data\\Export_Output.csv'
src = 'C:\\UC_Training_Areas'
dst = 'C:\\MOSAIC_Files'

fields = [f.name for f in arcpy.ListFields(fc)]
if f.type <> 'Geometry':
    for i,f in enumerate(fields):

        if f in (['FID', "Area", 'Category', 'SHAPE_Area']):
            fields.remove (f)    

with open(CSVFile, 'w') as f:
f.write(','.join(fields)+'\n') 
with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        f.write(','.join([str(r) for r in row])+'\n')

f.close()


columns = defaultdict(list) 
with open(CSVFile) as f:
  reader = csv.DictReader(f) 
  for row in reader: 
      for (k,v) in row.items(): 
         columns[k].append(v) 


print(columns['label'])
1
  • 1
    You may want to edit your code so that it is runnable via copy/paste. The indentation, etc... is off right now. Additionally, when you use the with(open(filename, 'w') as f:.... idiom, you do not need to use a f.close(). When you exit the with block the file will be closed automatically.. Finally, a question ... in this example is the text printed in the final line print(columns['label']) the name of the file that you want to copy to your destination directory ? Otherwise, I'm curious Commented Apr 27, 2015 at 1:29

3 Answers 3

3

Given the name of the file columns['label'] you can use the following to move a file

srcpath = os.path.join(src, columns['label'])
dstpath = os.path.join(dst, columns['label'])
shutil.copyfile(srcpath, dstpath)
Sign up to request clarification or add additional context in comments.

Comments

2

Here is the script I used to solve my problem:

import os
import arcpy
import os.path
import shutil
featureclass = "C:\\work_Data\\Export_Output.shp"
src = "C:\\Data\\UC_Training_Areas"
dst = "C:\\Data\\Script"

rows = arcpy.SearchCursor(featureclass)
row = rows.next()
while row:
     print row.Label
     shutil.move(os.path.join(src,str(row.Label)),dst)
     row = rows.next()

Comments

1

Think of it this ways way source and destination assuming you want to copy file from your picture folder to your image folder located somewhere in your machine destination
X is your machine name Z is the file name``

import os;
import shutil;
import glob;

source="C:/Users/X/Pictures/test/Z.jpg"
dest="C:/Users/Public/Image"

    if os.path.exists(dest):
    print("this folder exit in this dir")
else:
    dir = os.mkdir(dest)

for file in glob._iglob(os.path.join(source),""):
    shutil.copy(file,dest)
    print("done")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.