IO
raw_input and input
  • raw_input, return a string, has been removed and renamed as input in Python 3
  • input, return the result of the input expression, has been removed in Python 3, in which can be simulated by eval(input())
  • #!/usr/bin/python
    
    # return a string
    a = raw_input("Input an integer:\n");
    print type(a), a;
    
    # return the value of python expression
    b = input ("Input an integer:\n");
    print type(b), b;
    		
    Read
    #!/usr/bin/python
    
    # open file
    f = open('Generator.htm', 'rb');
    
    # read line by line
    for index, line in enumerate(f):
        print index, line,
    
    # back to starting position
    f.seek(0, 0);
    
    # display the current position
    print f.tell();
    
    # read line by line
    try:
        while True:
            print f.next(), # raise StopIteration when hit EOF
    except StopIteration:
        pass;
    
    f.seek(0, 0)
    # read character by character
    while True:
        c = f.read(1);
        if not c:
            break;
        print "%10s, %10d" % (c, ord(c));
    
    # close file
    f.close();
    		
    Write
    #!/usr/bin/python
    
    # open file
    f = open('Generator.htm', 'rb');
    o = open('temp.htm', 'wb');
    
    # read line by line
    for index, line in enumerate(f):
        s = '{:10d} {}'.format(index, line);
        o.write(s);
    
    f.close();
    o.close();
    		
    Reference