python - 根据 csv 文件创建的列表将文件从一个目录复制到另一个目录

标签 python python-2.7 python-3.x csv

我正在尝试根据 csv 文件创建的列表将文件从一个目录复制到另一个目录。现在我有了一个 csv 文件,其中有几列,但我设法提取了我需要的一列并将其保存在“imaglst”下。
该列表是扩展名为“.tif”的图像列表。我有一个输入文件夹(包含所有图像和额外图像)和一个输出文件夹(我想在其中复制“imaglst”中指定的图像)。

以下是当前代码:

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"

打印时保存的列表

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

现在我知道我错过了一些东西,但只是不知道是什么,任何建议或解决方法将不胜感激。当我运行它时,出现以下错误:

KeyError: '17251_0002_RGB'

所以,据我所知,我可能不会选择扩展名“.tif”,但我不确定。

最佳答案

问题出在最后的 for 循环中。

Python 循环实际上是 for-each 循环;在每次迭代中,i 是来自 imaglst 的实际值。您尝试使用它作为计数器来索引回列表,但您只需要使用该值。

此外,您随后直接调用的 shutil.copy 不会引用文件名。我希望您的意思是将 dir_src 与文件名一起加入。

最后,在对列表进行切片时,如果只是长度,则不需要包含端点。

所以,把它们放在一起:

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)

关于python - 根据 csv 文件创建的列表将文件从一个目录复制到另一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43933118/

相关文章:

python - Opsgenie powershell 警报帖子不起作用

python - 如何对 N 个元素的列表进行排序,然后用 -1 替换 0 到 N 之间的缺失值

Python:如何根据元组中指定的多个条件从 csv 文件中提取行

python - 将 python.exe 重命名为 python3.exe 以便与 Windows 上的 python2 共存

python - 如何在列表中附加最接近的值

python-3.x - 导入错误 : No module named 'keras'

python - 填充数据框中未明确说明的值

python - 如何使用 Python 在多个实例中将 CSV 中的重复项添加到特定行的末尾?

c# - 在 Ironpython 中安装 PsychoPy 作为第三方包

python - 如何获取矩阵中元素周围所有邻居的值?