How do I do this in Python (File Manipulation)?
Question: I have a bunch of HTML files in HTML folder. Those HTML files have unicode
characters which I solved by using `filter(lambda x: x in string.printable,
line)`. Now how do I write the changes back to the original file? What is the
best way of doing it? Each HTML file is of 30 kb in size.

    
    
      1 import os, string
      2 
      3 for file in os.listdir("HTML/"):
      4     print file
      5     myfile = open('HTML/' + file)
      6     fileList = myfile.readlines()
      9     for line in fileList:
     10         #print line
     11         line = filter(lambda x: x in string.printable, line)
     12     myfile.close()
    


Answer: Use the [fileinput](http://docs.python.org/library/fileinput.html#module-
fileinput) module. It allows you to read and write to the same file in place:

    
    
    import fileinput,sys,os
    files=[os.path.join('HTML',filename) for filename in os.listdir("HTML/")]
    for line in fileinput.input(files, inplace=True):    
        line = filter(lambda x: x in string.printable, line)
        sys.stdout.write(line)
    

