-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.py
30 lines (23 loc) · 824 Bytes
/
batch.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
#!/usr/bin/env python
import subprocess
import argparse
import re
parser = argparse.ArgumentParser(description='Batch change filenames')
parser.add_argument('inputFileName',metavar='baseNameIn')
parser.add_argument('outputFileName',metavar='baseNameOut')
args = parser.parse_args()
def runBash(cmd):
p = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
out = p.stdout.read().strip()
return out.split('\n')
def changeName(oldName,newNameBase):
temp = re.split('([0-9]+)',oldName)
newName = newNameBase + temp[1] + temp[2]
subprocess.call(["mv",oldName,newName])
def changeAllNames(oldNameBase,newNameBase):
files = runBash("ls")
for afile in files:
temp = re.split('([0-9]+)',afile)
if temp[0] == oldNameBase:
changeName(afile,newNameBase)
changeAllNames(args.inputFileName,args.outputFileName)