Skip to content

Instantly share code, notes, and snippets.

@zelark
Last active December 25, 2015 13:59
Show Gist options
  • Save zelark/6987276 to your computer and use it in GitHub Desktop.
Save zelark/6987276 to your computer and use it in GitHub Desktop.
Разносит файлы типа *_NN_* по директориям типа epdNN
#!/usr/bin/python3
import os
import re
from shutil import copyfile
# пути задаёшь как r'c:\path\path'
# хотя можно и так r'c:/path/path'
# путь к каталогам
DIR_WITH_EPD = r'cities'
# путь к файлам
DIR_WITH_FILES = r'temp'
dirs, files = {}, {}
isepd = re.compile(r'.*epd([a-z0-9]){2}$', re.IGNORECASE)
isfile = re.compile(r'.*_([a-z0-9]){2}_.*', re.IGNORECASE)
# поиск каталогов
for (dirname, subshere, fileshere) in os.walk(DIR_WITH_EPD):
match = isepd.match(dirname)
if match:
dirs[match.group(1)] = dirname
#print(dirs)
# поиск файлов
for filename in os.listdir(DIR_WITH_FILES):
match = isfile.match(filename)
if match:
if match.group(1) not in files:
files[match.group(1)] = [filename]
else:
files[match.group(1)].append(filename)
#print(files)
# копирование файлов по каталогам
for key, files in files.items():
for filename in files:
if key in dirs:
src = os.path.join(DIR_WITH_FILES, filename)
dst = os.path.join(dirs[key], filename)
try:
copyfile(src, dst)
print('[ok]', src, '=>', dst)
except:
print('[error]', src, '=>', dst)
else:
print('Кey [%s] not found.' % key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment