youandhubris
3/31/2018 - 12:04 AM

Combine Files

Python script example for passing arguments, combining a list of files, replacing some content and saving as a new file.

import os  
import re
import sys


# sys.args 
workingDir = sys.argv[1]
scriptVersion = sys.argv[2]


#scripts paths
scripts = [	"fileA.js",
			"fileB.js"]


#appending all the scripts inside the appender variable
appender = ''  
for script in scripts:  
    path = workingDir + '/' + script
    fileIn = open(path, 'rU')
    appender = appender + fileIn.read() + '\n'
    fileIn.close()

appender = appender.replace("Something Else", "Something " + scriptVersion)

#writing the content to file
fileOut = open(workingDir + '/Combined.js', 'w')  
fileOut.write(appender)  
fileOut.close()