for line in open('file.txt'):
print(re.sub('windows', 'linux', line))
or
print(re.sub('windows', 'linux', open('file.txt').read()))
Which one is better? Is there any differences?
BTW. Is is a good idea to manipulate a huge string with a regex?
As Polynomial said, the second one reads the whole file into RAM, which may or may not be better.
But there is another solution:
This ensures that the file will be closed immediately after not needing it any longer.
Similiarly,
can be used as well.