组织文件并将文件复制到新文件夹

时间:2015-10-01 15:38:26

标签: python regex directory file-management

我在处理之前尝试组织一些数据。

我所拥有的是原始tiff文件的文件夹(它们是来自无人机传感器的光栅带)。 Example of file structure

我想将这些文件移动到新的单个文件夹中。例如,IMG_001_1,IMG_001_2,IMG_001_3,IMG_001_4和IMG_001_5都被移动到名为IMG_001的新文件夹中。我可以更改文件的命名结构,以使代码更简单。

另一个问题是文件夹中缺少一些图像。当前文件是IMG0016 - IMG0054(无IMG0055),IMG0056 - IMG0086(无IMG0087)和IMG0087 - IMG0161。这就是为什么我认为从1-143重命名新的图像文件夹会更简单。

我的主要问题是将文件移动到新文件夹中 - 创建文件夹非常简单。

2 个答案:

答案 0 :(得分:1)

玩了一下,这个剧本出来了,应该做你想做的事情:

import os
import shutil
import re

UNORG = "C:\\Users\joshuarb\Desktop\Unorganized_Images\\"
ORG = "C:\\Users\joshuarb\Desktop\Organized_Images\\"


def main():
    file_names = [os.path.join(UNORG, i) for i in get_files_of(UNORG)]
    for count in range(0, 143):
        current_dir = "{}IMG_{:04d}".format(ORG, count)
        os.makedirs(current_dir)
        move_files = get_files_to_move(file_names, count)
        print move_files
        for i in move_files:
            shutil.move(i, os.path.join(current_dir, os.path.basename(i)))


def get_files_to_move(file_names, count):
    return [i for i in file_names if re.match('.*IMG{}_.*'.format(count), i)]


def get_files_of(mypath):
    (dirpath, dirnames, filenames) = os.walk(mypath).next()
    return filenames


if __name__ == '__main__':
    main()

如您所见,代码未被注释。但随便问一下是否有不清楚的事情;)

答案 1 :(得分:0)

问题解决了!

var customIcon = new L.Icon({iconUrl: 'https://www.mapbox.com/mapbox.js/assets/images/astronaut1.png', iconSize: [50, 50], iconAnchor: [25, 25], popupAnchor: [0, -25], className: 'dot' })
L.mapbox.featureLayer(
{
    type: 'Feature',
    geometry: { type:'Point', coordinates:[$longitude, $latitude] },
    properties: { title:'My Location', description:'My Description'}
},
{
    pointToLayer: function(feature,latLng) { return L.marker(latLng, {icon: customIcon}}
 }).addTo(map);
相关问题