reading files in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ananthakrishna
    New Member
    • Feb 2010
    • 1

    reading files in python

    I wrote a script for finding the characters and the file size
    The value returned by os.path.getsize () is bytes only
    When I tried to read the charecters it is giving less than the value given by the function.
    Code:
    import os
    File=open("C:\\df.txt",'r')
    File.seek(0)
    size=os.path.getsize("C:\\df.txt")
    print "size-----"+str(size)
    count=0
    for c in File.read():
            count=count+1
    print "characters------"+str(count)
    output:

    size-----679
    charecters------662


    why is this difference b/w the charecters and the size
    Last edited by bvdet; Feb 18 '10, 01:32 PM. Reason: Add code tags
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    I've done a little testing and it appears the difference between the size and the number of characters counted is equal to the number of newlines in a file. So I'm guessing this is related the fact that Windows uses 2 characters to indicate the end of a line.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      View a text file in Notepad++ and show all characters (Windows). Each newline character ("\n") is actually 2 characters (CR and LF). That is the difference.

      Note: ninja'd by YarOfDoom!

      Comment

      Working...