1

There is getsizeof function from sys module but it includes object overhead.

I already looked at similar questions on stackoverflow but I couldn't find what I'm asking.

    with open(args.csvfile) as f:
        for line in f:
            print(sys.getsizeof(line))

File itself is 30 bytes and first line is 61 bytes (because of object overhead). Is here a way to just get the size of the data without object overhead?

5
  • Do you want the length of the string? Use len() if you want the length of the string. Commented Sep 24, 2019 at 12:08
  • 1
    "File itself is 30 bytes", I'm assuming you refer to the on-disk size of the file? That one depends also on the encoding. An indicative measure of the size could be len(f.read().encode('utf-8')), but this only tells you how many bytes the file content needs to be encoded (and that's assuming the stored file is encoded in UTF-8, which is not necessarily true). Commented Sep 24, 2019 at 12:11
  • @Kevin no I was thinking of size but thanks Commented Sep 24, 2019 at 12:17
  • @GPhilo yes that adds up to 28 and 2 left for whatever the rest is probably metadata. Thanks! Commented Sep 24, 2019 at 12:18
  • if you need size of file then you have os.stat(filename).st_size Commented Sep 24, 2019 at 12:19

1 Answer 1

1
with open(args.csvfile, 'rb') as f:
    data = f.read()
    print(len(data))  # size in bytes
Sign up to request clarification or add additional context in comments.

3 Comments

@Kevin not exactly. This is the length of the byte string read from the file, which is effectively the file size. This is different from the string length, because strings are encoded.
It sounds like OP is looking for the number 30 though (28 + 2), though this is just the size of the data, not the actual size on disc.
If you are looking for the exact match from dir command, then this should do it. Try it and see if it matches in your use case. My file reads 24,987 from this script, on Windows file properties, and with dir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.