Reading txt files in python
Question: I'm currently following "Learn Python the Hard Way". However, when I use the
.read() command on my .txt files it outputs the text in a very weird way, with
extra spaces, and a square at the start:

![Extra spaces and squares.](http://i.stack.imgur.com/s0ET7.png)

The console is Windows Powershell.

My code looks like this:

    
    
    from sys import argv #imports argv from sys
    
    script, filename = argv #unpacks script and filename from argv
    
    txt = open(filename) #declares the variable txt as the text in filename
    
    print "Here's your file %r" % filename #prints the string and the filename
    print txt.read() #prints a reading of txt
    txt.close()
    
    print "Type the filename again:" #prints the string
    file_again = raw_input("> ") #declares the variable file_again as the raw input
    
    txt_again = open(file_again) #declares the variable txt_again as the text in file_again
    
    print txt_again.read() #prints a reading of txt_again
    txt.close()
    

And the files looks like this:

    
    
    This is stuff I typed into a file.
    It is really cool stuff.
    Lots and lots of fun to have in here.
    

Please help!


Answer: Your file seems to be encoded with a 2-Byte encoding; presumably UTF-16. Since
python can't guess that, it just outputs the bytes as it gets them; for ASCII-
only text, this means that every other character is plain-text readable.

