Reading a single character from a file in python?

file-io, python

Solution

This is one way:

with open(filename) as f:
    for line in f:
        for c in line:
            pass

Or what about this?

with open(filename) as f:
    for c in f.read():
        pass

Problem

My question would be if there was any other way besides below to iterate through a file one character at a time? ``` with open(filename) as f: while True: c = f.read(1) if not c: print "End of file" break print "Read a character:", c ``` Since there is not a function to check whether there is something to read like in Java, what other methods are there. Also, in the example, what would be in the variable c when it did reach the end of the file? Thanks for anyones help.

Original source