Copy certain files from one folder to another using python

2024/10/18 13:25:56

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'])
Answer

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)
https://en.xdnf.cn/q/72922.html

Related Q&A

I want to create django popup form in my project [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

SVG to PNG with custom fonts in Python

Im using Cairo/RSVG based solution for rasterizing SVG to PNG. Its already beeb described on StackOverflow in Convert SVG to PNG in Python. However, this solution doesnt seem to work with custom fonts.…

How to solve an equation with variables in a matrix in Python?

im coding in Pyhon, and Im working on stereo-correlation. I want to resolve this equation : m = K.T.Mm,K,M are know.where :M is the homogeneous coordinate of a point in the cartesian coordinate system…

how to create a new method with signature of another

How can I copy the signature of a method from one class, and create a "proxy method" with same signature in another ?.I am writing a RPC library in python. The server supports remote calls t…

Converting a full column of integer into string with thousands separated using comma in pandas

Say I have population data stored in a column of a dataframe using pandas in python with Country names as row indices. How do I convert the whole column of numbers into string thousands separator using…

Nested WHILE loops in Python

I am a beginner with Python and trying few programs. I have something like the following WHILE loop construct in Python (not exact). IDLE 2.6.4 >>> a=0 >>> b=0 >>> whil…

Fastest way to drop rows / get subset with difference from large DataFrame in Pandas

Question Im looking for the fastest way to drop a set of rows which indices Ive got or get the subset of the difference of these indices (which results in the same dataset) from a large Pandas DataFram…

Python inheritance: when and why __init__

Im a Python newbie, trying to understand the philosophy/logic behind the inheritance methods. Questions ultimately regards why and when one has to use the __init__ method in a subclass. Example:It seem…

TypeError: A Future or coroutine is required

I try make auto-reconnecting ssh client on asyncssh. (SshConnectManager must stay in background and make ssh sessions when need)class SshConnectManager(object): def __init__(self, host, username, passw…

Python socket closed before all data have been consumed by remote

I am writing a Python module which is communicating with a go program through unix sockets. The client (the python module) write data to the socket and the server consume them.# Simplified version of t…