【问题标题】:Copying files from one directory to another based on a list created from a csv file根据从 csv 文件创建的列表将文件从一个目录复制到另一个目录
【发布时间】:2017-10-11 12:01:50
【问题描述】:

我正在尝试根据从 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"

打印时保存的列表

打印 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”,但我不确定。

【问题讨论】:

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


    【解决方案1】:

    问题出在最后的 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)
    

    【讨论】:

    • ,shutil join ,这还会把文件复制过来吗?我刚刚尝试了您放在一起查看结果的脚本,得到错误“TypeError:关键字参数itemslikeregex 是互斥的”。这个想法是让它遍历列表并从 "dir_src" 中找到相同的命名图像,从 "dir_src" 复制它们并将它们粘贴到将指定的 "dir_dst" 中。
    • 您应该发布完整的回溯。这似乎是 Pandas 的一个错误,不幸的是我对此一无所知。
    • 回溯(最近一次调用最后):文件“”,第 1 行,在 runfile('J:/Documents/RD/Height_team project/image_locator_copier .py', wdir='J:/Documents/RD/Height_team project') 文件 "C:\Users\edwinp\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py ",第 880 行,在运行文件 execfile(filename, namespace) 文件“C:\Users\edwinp\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py”中,第 87 行,在 execfile 中 exec(compile(scripttext, filename, 'exec'), glob, loc)
    • 文件 "J:/Documents/RD/Height_team project/image_locator_copier.py",第 35 行,在 中用于 imaglst.filter(os.listdir(dir_src), i) 中的文件名:文件“C:\Users\edwinp\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\generic.py”,第 2451 行,在过滤器中引发 TypeError('关键字参数itemslike,或regex ' TypeError: 关键字参数itemslikeregex 是互斥的
    • @Daniel 那将是完整的 Traceback,必须创建两个 cmets,达到字符限制。这对我来说真的没有意义
    【解决方案2】:

    这似乎使它起作用了!

    来自我之前的条目:

    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)
    

    我改成:

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

    现在效果很好,通过它从 csv 创建的列表并在指定的文件夹中查找扩展名为“.tif”的图像并将其复制到指定的位置文件夹。

    感谢@Daniel Roseman 的帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-15
      • 2017-11-18
      • 2013-06-01
      • 2020-06-22
      • 2014-08-12
      • 2013-10-24
      • 2011-10-24
      相关资源
      最近更新 更多