forked from dbenn/photometry_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeros-and-offset.py
42 lines (33 loc) · 985 Bytes
/
zeros-and-offset.py
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
# Remove leading zeros from file names in the current directory, subtract
# an optional offset value from the sequence number in each, and copy the
# file to a temporary directory.
#
# Nov 2014, [email protected]
import os
import re
import shutil
import sys
def main():
files = os.listdir('.')
filepat = re.compile('([^0-9]+)([0-9]+)\.(\w+)')
if len(sys.argv) == 2:
offset = int(sys.argv[1])
else:
offset = 0
dest_dir = 'temp'
if os.path.exists(dest_dir):
os.rmdir(dest_dir)
os.mkdir(dest_dir)
for file in files:
match = filepat.match(file)
if match is not None:
seq_num = int(match.group(2))
new_seq_num = seq_num - offset
prefix = match.group(1)
suffix = match.group(3)
new_file = "{0}{1}.{2}".format(prefix, new_seq_num, suffix)
new_path = os.path.join(dest_dir, new_file)
shutil.copyfile(file, new_path)
print("{0} => {1}".format(file, new_path))
if __name__ == "__main__":
main()