简体   繁体   中英

Organizing and copying files to new folders

I'm trying to organize some data before processing it.

What I have is a folder of raw tiff files (they're raster bands from a drone sensor). 文件结构示例

I want to move these files into new, individual folders. eg, IMG_001_1, IMG_001_2, IMG_001_3, IMG_001_4 and IMG_001_5 are all moved into a new folder titled IMG_001. I am ok with changing the naming structure of the files in order to make the code simpler.

An additional issue is that there are a few images missing from the folder. The current files are IMG0016 - IMG0054 (no IMG0055), IMG0056 - IMG0086 (no IMG0087), and IMG0087 - IMG0161. This is why I think it would be simpler to just rename the new image folders from 1-143.

My main problem is actually moving the files into the new folders - creating the folders is fairly simple.

Played around a little and this script came out, which should do what you want:

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()

As you see, the code is not commented. But feel free to ask if something is unclear;)

Problem solved!

import os
import shutil

srcpath = "C:\Users\joshuarb\Desktop\Python_Test\UnorganizedImages"
srcfiles = os.listdir(srcpath)

destpath = "C:\Users\joshuarb\Desktop\Python_Test\OrganizedImages"

# extract the three letters from filenames and filter out duplicates
destdirs = list(set([filename[0:8] for filename in srcfiles]))


def create(dirname, destpath):
    full_path = os.path.join(destpath, dirname)
    os.mkdir(full_path)
    return full_path

def move(filename, dirpath):
    shutil.move(os.path.join(srcpath, filename)
            ,dirpath)

# create destination directories and store their names along with full  paths
targets = [
    (folder, create(folder, destpath)) for folder in destdirs
]

for dirname, full_path in targets:
    for filename in srcfiles:
        if dirname == filename[0:8]:
           move(filename, full_path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM