forked from AllenDowney/ThinkPython
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsed.py
46 lines (33 loc) · 926 Bytes
/
sed.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
37
38
39
40
41
42
43
44
45
46
"""This module contains code from
Think Python by Allen B. Downey
http://thinkpython.com
Copyright 2012 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
import sys
def sed(pattern, replace, source, dest):
"""Reads a source file and writes the destination file.
In each line, replaces pattern with replace.
pattern: string
replace: string
source: string filename
dest: string filename
"""
try:
fin = open(source, 'r')
fout = open(dest, 'w')
for line in fin:
line = line.replace(pattern, replace)
fout.write(line)
fin.close()
fout.close()
except:
print 'Something went wrong.'
def main(name):
pattern = 'pattern'
replace = 'replacendum'
source = name
dest = name + '.replaced'
sed(pattern, replace, source, dest)
if __name__ == '__main__':
main(*sys.argv)