Python code not reading file, says file has no lines
Question: I had an assignment due that asked to read a file and display the top 10 lines
of the file, or all of the lines if the file is less than 10 lines long. When
I tried to run my code on my computer, it registered the file as having 0
lines (no matter what file I used) and displayed only a blank line as output.
I would like to understand where I went wrong so I can avoid my mistake for
the next assignment. Any style or other tips are also welcome.

Here is my code:

    
    
    #Displays the top 10 lines in a file
    
    import sys
    
    # Make sure the input is correct, take file name
    if len(sys.argv) == 2:
        filename = sys.argv[1]
    else:
        print("You must start the program with 1 command line parameter.")
        quit()
    
    # open file    
    fle = open(filename, "r")
    
    #Count number of lines
    linecount = 0
    for line in fle:
        linecount = linecount + 1
    
    # If the file is less than 10 lines, print the entire file
    # If the file has more than 10 lines, print only first 10.
    lines = fle.readlines()
    if linecount < 10:
        for line in fle:
            print(line,)        
    else:
        for i in range(10):
            print(lines[i])
    


Answer: Could it be that

    
    
    for line in fle:
        linecount = linecount + 1
    

reads every line from the file so that, after the loop is done, there are no
more lines to read from that file in `lines = fle.readlines()`?

Try inserting a `fle.seek(0)` before `lines = fle.readlines()` to "rewind" the
file to the beginning before re-reading it.

(See also [here](http://stackoverflow.com/questions/10255273/iterating-on-a-
file-using-python) for example.)

