Freeman Page
Freeman Page

Reputation: 11

Copying files from one directory to another based on a list created from a csv file

I am trying copying files from one directory to another based on a list created from a csv file. Now I have got a csv file which had several columns but I managed to extract one column I need and save it under a 'imaglst'.
The list is a list of images '.tif' extension. I have got an input folder (with all images in and extra) and an output folder (where I want to copy the images in that are specified in the 'imaglst').

Below is current code:

import os
import glob
import pandas
import shutil


cwd = os.getcwd()
path = cwd
extension = 'csv'
result = [i for i in glob.glob('*.{}'.format(extension))]
print result

colnames = ['X' ,'Y' ,'ATTR_1',' ATTR_2',' ATTR_3 ','ATTR_4' ,'ELEVATION']
data = pandas.read_csv(result[0],names=colnames, delimiter=r"\s+")
imaglst = data['ATTR_1']
print imaglst[1:len(imaglst)]

dir_src = raw_input('enter the full image folder location :')
dir_dst = raw_input('enter the full output folder location :')

for i in imaglst[1:len(imaglst)]: 
    for filename in imaglst.filter(os.listdir(dir_src), imaglst[i]):
        shutil.copy(dir_src, dir_dst)



print "---------------------------------------------------------------------------------"
print "Process competed"

The list it saves when I print

print imaglst[1:len(imaglst)]

1      17251_0002_RGB
2      17251_0004_RGB
3      17251_0006_RGB
4      17251_0008_RGB
5      17251_0010_RGB
6      17251_0012_RGB
7      17251_0014_RGB
8      17251_0016_RGB
9      17251_0018_RGB
10     17251_0020_RGB
11     17251_0022_RGB
12     17251_0024_RGB
13     17251_0026_RGB
14     17251_0028_RGB
15     17251_0030_RGB
16     17251_0032_RGB
17     17251_0034_RGB
18     17251_0036_RGB
19     17251_0038_RGB
20     17251_0040_RGB
21     17251_0042_RGB
22     17251_0044_RGB
23     17251_0046_RGB
24     17251_0048_RGB
25     17251_0050_RGB
26     17251_0052_RGB
27     17251_0054_RGB
28     17251_0056_RGB
29     17251_0058_RGB
30     17251_0060_RGB

206    17005_0114_RGB
207    17005_0116_RGB
208    17005_0118_RGB
209    17005_0120_RGB
210    17005_0122_RGB
211    17005_0124_RGB
212    17005_0126_RGB
213    17005_0128_RGB
214    17005_0130_RGB
215    17005_0132_RGB
216    17005_0134_RGB
217    17005_0136_RGB
218    17005_0138_RGB
219    17005_0140_RGB
220    17005_0142_RGB
221    17005_0144_RGB
222    17005_0146_RGB
223    17005_0148_RGB
224    17005_0150_RGB
225    17005_0152_RGB
226    17005_0154_RGB
227    17005_0156_RGB
228    17005_0158_RGB
229    17005_0160_RGB
230    17005_0162_RGB
231    17005_0164_RGB
232    17005_0166_RGB
233    17005_0168_RGB
234    17005_0170_RGB
235    17005_0172_RGB

Now I know that I am missing something but just don't know what, any advice or workaround would be appreciated. When I run it I get following error:

KeyError: '17251_0002_RGB'

So, what I can understand of it that I might not be picking up the extension ".tif", but I am not sure.

Upvotes: 1

Views: 1932

Answers (2)

Freeman Page
Freeman Page

Reputation: 11

This seemed to make it work!

from my previous entry:

for i in imaglst[1:]: 
    for filename in imaglst.filter(os.listdir(dir_src), i):
        shutil.copy(os.path.join(dir_src, filename), dir_dst)

I changed to:

for i in imaglst[1:]: 
    shutil.copy(os.path.join(dir_src, i+ '.tif'), dir_dst)

Which works great now, goes through a list that it created from csv and looks for images with extension '.tif' in folder specified and copies it to the location folder specified.

Thanks for the help @Daniel Roseman

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599490

The problem is in your for loop at the end.

Python loops are really for-each loops; in each iteration, i is the actual value from imaglst. You're trying to use that as a counter to index back into the list, but you just need to use the value.

Also, your shutil.copy call directly afterwards doesn't reference the filename. I expect you mean to join dir_src with the filename.

Finally, when slicing a list you don't need to include the endpoint if it is just the length.

So, putting it all together:

for i in imaglst[1:]: 
    for filename in imaglst.filter(os.listdir(dir_src), i):
        shutil.copy(os.path.join(dir_src, filename), dir_dest)

Upvotes: 1

Related Questions