-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsed.py
More file actions
32 lines (25 loc) · 872 Bytes
/
Copy pathsed.py
File metadata and controls
32 lines (25 loc) · 872 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
30
31
32
import sys
def sed( pattern, replace, source, target ):
try:
fin = open( source )
except Exception as e:
print( "Error occurred while attempting to open source file." , file = sys.stderr)
exit()
try:
fout = open( target, 'w' )
except Exception as e:
print( "Error occurred while attempting to open target file.", file = sys.stderr )
exit()
for line in fin:
newline = ""
findstr = line.find( pattern )
cursor = 0
while findstr >= 0 :
newline += line[ cursor: findstr ]
newline += replace
cursor = findstr + len( pattern )
findstr = line[ cursor : ].find( pattern )
newline += line[ cursor : ]
fout.write( newline )
if __name__ == "__main__":
sed( "duck", "fuck", "./test.txt", "./testout.txt" )