Skip to content

Instantly share code, notes, and snippets.

@henryscala
Created November 24, 2014 05:57
Show Gist options
  • Save henryscala/29db7ba44c73eead0011 to your computer and use it in GitHub Desktop.
Save henryscala/29db7ba44c73eead0011 to your computer and use it in GitHub Desktop.
python script for changing line in a file in patch way
srcFileName = r'file1.java'
dstFileName = r'file2.java'
srcFile = open(srcFileName,'r')
dstFile = open(dstFileName,'w')
def listToStr(alist):
result ='';
for a in alist:
result += ', %s' % a
return result
def processLine(line):
try:
indexFun = line.index('someFunction')
except:
return line
indexLeftBracket = line.index('(')
indexRightBracket = line.index(')')
allArguments = line[indexLeftBracket+1:indexRightBracket]
argumentList = allArguments.split(',')
if len(argumentList) != 3:
return line
newLine = line[0:indexLeftBracket]+'('+argumentList[0]+', '+argumentList[1] + ', '+argumentList[1] +', '+argumentList[2] +');\n'
return newLine
for line in srcFile.readlines():
newLine = processLine(line)
dstFile.write(newLine)
srcFile.close()
dstFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment