#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    24
    Rep Power
    0

    Problem calculating file sizes


    Just to give a little background, I'm trying to create a progress bar that shows the status of a load operation.

    I'm using os.path.getsize() to get the size of my file. In Windows, the file is 8,345 KB, and the getsize method returns 8544455, which is correct. But, when I read the file with the following:

    Code:
    path = '/path/to/file'
    read = 0
    with open(path, 'r') as handle:
      for line in handle:
        read += len(line)
        file.append(handle.readline().strip('\n'))
    I am not getting the same length as the os.path.getsize() returns. Actually, I get around half the size of the file. I end up with around 4253313 bytes. If I multiply by 2, I get very close to the actual size, but its not exact, so my progress bar doesn't fill up to 100%.

    Should I use something other than len() to get the number of bytes in the file?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    569
    Rep Power
    35
    Print "line" and the result of the readline and I think you will see the problem. It should be
    Code:
    with open(path, 'r') as handle:
      for line in handle:
        read += len(line)
        file.append(line.strip('\n'))
    Also, once your progress gets to 98 or 99%, or if it is over 100%, change the value to 100% to avoid slight errors showing.

IMN logo majestic logo threadwatch logo seochat tools logo