-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplyRegexToFileNames.py
36 lines (31 loc) · 1 KB
/
applyRegexToFileNames.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
# Import all dependencies
import re, os, sys, shutil
inputFolder = ''
outputFolder = ''
regex = ''
replacement = ''
parsedFile = ''
def readInput():
global inputFolder
global outputFolder
global regex
global replacement
inputFolder = input('>> Input folder : ')
outputFolder = input('>> Output folder : ')
regex = input('>> Regex : ')
replacement = input('>> Replacement : ')
def createParsedOutput(dirname, oldFilename):
newFilename = re.sub(regex, replacement, oldFilename)
oldFilePath = os.path.join(dirname, oldFilename)
outputFileDir = outputFolder + dirname[len(inputFolder):]
newFilePath = os.path.join(outputFileDir, newFilename)
if not os.path.exists(outputFileDir):
os.mkdir(outputFileDir)
shutil.copyfile(oldFilePath, newFilePath)
print(oldFilePath + ' ---> ' + newFilePath)
readInput()
# Browse all files and subfolders
for dirname, dirnames, filenames in os.walk(inputFolder):
# Browse all files in current subfolder
for filename in filenames:
createParsedOutput(dirname, filename)