1
1
#!/usr/bin/python
2
2
# -*- coding: UTF-8 -*-
3
3
4
+ from __future__ import print_function
4
5
import sys
5
6
import getopt
6
7
import re
@@ -21,21 +22,21 @@ def insertBefore(filename, pattern, text):
21
22
with open (filename , 'r+' ) as fp :
22
23
line_number = findLine (pattern , fp )
23
24
if (line_number > 0 ):
24
- print 'Insert' , text ,'to line' , line_number
25
+ print ( 'Insert' , text ,'to line' , line_number )
25
26
fp .seek (0 )
26
27
lines = fp .readlines ()
27
28
fp .seek (0 )
28
29
lines .insert (line_number - 1 , text + '\n ' )
29
30
fp .writelines (lines )
30
31
return
31
- print 'pattern' ,text ,'not found!'
32
+ print ( 'pattern' ,text ,'not found!' )
32
33
33
34
def replaceText (filename , pattern , text ):
34
35
with open (filename , 'r' ) as fp :
35
36
lines = fp .read ()
36
37
fp .close ()
37
38
lines = (re .sub (pattern , text , lines ))
38
- print 'Replace' , pattern ,'to' , text
39
+ print ( 'Replace' , pattern ,'to' , text )
39
40
fp = open (filename , 'w' )
40
41
fp .write (lines )
41
42
fp .close ()
@@ -48,11 +49,11 @@ def main(argv):
48
49
try :
49
50
opts , args = getopt .getopt (argv , "hi:s:t:r" )
50
51
except getopt .GetoptError :
51
- print 'add-line.py -i <inputfile> -s <string> -t <text>'
52
+ print ( 'add-line.py -i <inputfile> -s <string> -t <text>' )
52
53
sys .exit (2 )
53
54
for opt , arg in opts :
54
55
if opt == '-h' :
55
- print 'add-line.py -i <inputfile> -s <string> -t <text>'
56
+ print ( 'add-line.py -i <inputfile> -s <string> -t <text>' )
56
57
sys .exit ()
57
58
elif opt in ("-i" ):
58
59
inputfile = arg
0 commit comments