Last active
April 27, 2018 11:29
-
-
Save jezman/94ee3eb3b3a01c16fa7a4b71c5ab7dec to your computer and use it in GitHub Desktop.
Moves files with numbers in the file to subfolder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import os | |
from sys import argv | |
parser = argparse.ArgumentParser() | |
parser.add_argument("file", help='file with shots numbers') | |
parser.add_argument("folder", help='folder to scan') | |
args = parser.parse_args() | |
FILE_WITH_NUMBERS = argv[1] | |
TMP_FILE = '/tmp/nums.txt' | |
DIR = argv[2] | |
CONVERTING = DIR + '/for_processing/' | |
def replace(): | |
with open(TMP_FILE, 'w') as o, open(FILE_WITH_NUMBERS, 'r') as i: | |
for line in i: | |
if not line.startswith('IMG'): | |
o.write('IMG_' + line.rstrip('\n') + '.CR2' + '\n') | |
if os.stat(TMP_FILE).st_size != 0: | |
os.rename(TMP_FILE, FILE_WITH_NUMBERS) | |
else: | |
os.remove(TMP_FILE) | |
return [line.strip() for line in open(FILE_WITH_NUMBERS)] | |
def move(nums): | |
for root, dirs, files in os.walk(DIR): | |
if not os.path.exists(CONVERTING): | |
os.mkdir(CONVERTING) | |
for f in files: | |
if f in nums: | |
os.rename(DIR + f, CONVERTING + f) | |
if __name__ == '__main__': | |
if not DIR.endswith('/'): | |
DIR = DIR + '/' | |
move(replace()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment