Nice ihatehippies, I only unified the with to produce lines to output immediately to avoid temporary list (if you do lot of processing it might still pay off to use one and output from that), changed the name aDirectory to follow PEP8 convetion of words joined by underscore and removed unnecessary readlines as we can use fileobj directly as generator of lines.
import os
html_directory = r'I:\python27\Lib\site-packages\pygame\docs\ref'
pre = 'absatz'
for name in os.listdir(html_directory):
if name.lower().endswith('.html') and not name.startswith(pre):
with open(os.path.join(html_directory, name), 'rb') as fileobj, open(os.path.join(html_directory, '%s_%s' % (pre, name)), 'wb') as output:
output.write(''.join(line.replace("Paragraph", "Absatz")
for line in fileobj if not "embed_music" in line))
Of course you never should use this kind of code for html file but use Beautifulsoup3/Beautifulsoup4 or similar to preserve the structure of files.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852