-2

I have following code

import StringIO
import os
output = StringIO.StringIO("c:/temp/file.txt")  
position = output.tell()
output.seek(0, os.SEEK_END)
size = output.tell()
output.seek(position, os.SEEK_SET) 
print size

It displays size as 22 But when I run following , it displays size as 607011L

os.stat("c:/temp/file.txt")).st_size 

Is there a way to convert 22 from StringIO size to actual size 607011L of file

user3376169
  • 405
  • 1
  • 5
  • 17
  • Read [the docs](https://docs.python.org/2/library/stringio.html) and it'll be *really* clear why this doesn't work (`StringIO` has nothing to do with *paths* or actual *files*). Side-note: Don't learn Python 2. It's a dead language, reaching end of life over a year ago. Learn Python 3 now, and save the effort of relearning it later. – ShadowRanger Mar 12 '21 at 01:03
  • That displays 16, not 22. – superb rain Mar 12 '21 at 01:09

1 Answers1

1

Because StringIO does not read the file. What you have done there is created a fake file that contains that string as its contents. If you want to read the real file, use open. If you want something that acts like a file but uses a string in memory, use StringIO.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30