简体   繁体   English

使用python将某些文件复制到另一个文件夹

[英]Copy certain files from one folder to another using python

I am trying to copy only certain files from one folder to another. 我试图只将某些文件从一个文件夹复制到另一个文件夹。 The filenames are in a attribute table of a shapefile. 文件名位于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. 我成功将文件名写入.csv文件并列出包含要传输的文件名列表的列。 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. 我已阅读有关使用Shutil.copy / move但不确定如何使用它的内容。 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'])

Given the name of the file columns['label'] you can use the following to move a file 给定文件columns['label']的名称,您可以使用以下内容移动文件

srcpath = os.path.join(src, columns['label'])
dstpath = os.path.join(dst, columns['label'])
shutil.copyfile(srcpath, dstpath)

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()

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`` X是您的机器名称Z是文件名``

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中使用哪种文件shutil复制方法将某些文件从一个文件夹复制到另一个文件夹? - Which file shutil copy method to use in python to copy certain files from one folder to another? 如何使用 Python 将 zipfile 从一个文件夹复制到另一个文件夹 - How to copy zipfile from one folder to another folder using Python 使用 python 在 AWS S3 上将文件从一个文件夹复制到另一个文件夹 - copy files from one folder to another on AWS S3 with python 使用python脚本将一个文件夹中的多个jpeg文件和json文件复制并排序到子文件夹文件夹中的另一个文件夹 - copy and sort multiple jpeg files and json files in one folder to another folder in subfolders folder using python script 有没有办法使用 python 中该文件名的子文本将文件从一个文件夹复制到另一个文件夹? - Is there a way to copy files from one folder to another using a subtext of that file name in python? 我正在编写 python 脚本,通过将一行中的多个输入复制到另一个文件夹,从一个文件夹复制某些多个图像文件 (.jpg) - I am writing the python script for copy the certain multiple image files (.jpg)from one folder by giving multiple input in one line to another folder 如何使用 Python 将图像从一个文件夹复制到另一个文件夹? - How to copy images from one folder to another using Python? 使用python将文件复制到另一个文件夹 - Copy files to another folder using python 如何将一种特定类型的所有文件从一个文件夹复制到Python中的另一个文件夹 - How do I copy all files of one specific type from a folder to another folder in Python 使用Python将某些文件复制到其他文件夹(Python中的文件管理) - Copy certain files to other folder using Python ( File Management in Python )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM