-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbatchRename.py
More file actions
executable file
·31 lines (24 loc) · 999 Bytes
/
Copy pathbatchRename.py
File metadata and controls
executable file
·31 lines (24 loc) · 999 Bytes
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
#parse command line arguments
parser = argparse.ArgumentParser(description='Batch change filename.')
parser.add_argument('inputFileName', metavar='baseNameIn')
parser.add_argument('outputFileName', metavar='baseNameOut')
args = parser.parse_args()
#Same as previous
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 = oldName.split('.')
newName = newNameBase + '.' + temp[1] + '.' + temp[2]
subprocess.call(["mv", oldName, newName])
def changeAllNames(oldNameBase,newNameBase):
files = runBash("ls")
for afile in files:
if afile.split('.')[0] == oldNameBase:
changeName(afile,newNameBase)
#change files with base inputFileName to base outputFileName
changeAllNames(args.inputFileName, args.outputFileName)