-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumcp.py
More file actions
executable file
·77 lines (54 loc) · 2 KB
/
numcp.py
File metadata and controls
executable file
·77 lines (54 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# numcp - copy files to sequentially numbered files
from optparse import OptionParser
import re
from os.path import *
from shutil import copy2
import operator
usage = """usage: %prog [options] src [src src src...] DESTDIR
%prog: Copy source files to sequentially numbered files in DESTDIR
Examples:
%prog foo/a foo/b bar/c quux
Copies the three files foo/a.txt, foo/b.zip, and bar/c to the
directory quux, as files 0000.txt, 0001.zip, and 0002, respectively.
%prog -a 10 foo/* quux
Copies the files in foo/ to quux, starting the numbering with 10
rather than 0."""
parser = OptionParser(usage=usage)
parser.add_option("-f", "--format", dest="format", default=None,
help="""The format for the name of the destination files. For example,
"%04d.jpg". The default is to copy each file to a 4-digit number, zero-padded,
with the same extension as the original file.""")
parser.add_option("-a", "--add", dest="offset", type="int", default=0,
help="OFFSET is added to each destination's number")
parser.add_option("-s", "--sort", action="store_true", default=False,
dest="sort",
help="Do a numerical sort on the input files before assigning them numbers")
(opts, args) = parser.parse_args()
if len(args) < 2:
parser.print_help()
exit(1)
destdir = args[-1]
srcfiles = args[0:-1]
def name_to_number(file_name):
"""Extracts the last number found in a string.
For instance, name_to_number("img_234.jpg") would return 234."""
numbers = re.findall(r"(\d+)", file_name)
if numbers:
return int(numbers[-1])
else:
return None
if opts.sort:
srcfiles.sort(key=name_to_number)
for (index, src) in enumerate(srcfiles):
index = index + opts.offset
if opts.format:
dest = join(destdir, opts.format % index)
else:
if '.' in src:
ext = '.' + src.rsplit('.', 1)[1]
else:
ext = ''
dest = join(destdir, ('%04d' % index) + ext)
print src, "->", dest
copy2(src, dest)