问题描述
我正在尝试仅将某些文件从一个文件夹复制到另一个文件夹.文件名在shapefile的属性表中.
I am trying to copy only certain files from one folder to another. The filenames are in a attribute table of a shapefile.
我成功地将文件名写入.csv文件,并列出了包含要传输的文件名列表的列.在那之后,我被困在如何读取这些文件名以将它们复制到另一个文件夹的问题上.我已经阅读过有关使用Shutil.copy/move的信息,但不确定如何使用它.任何帮助表示赞赏.下面是我的脚本:
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'])
推荐答案
给出文件名columns['label']
您可以使用以下内容移动文件
Given the name of the filecolumns['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)
这篇关于使用python将某些文件从一个文件夹复制到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!