-1

I am trying to get the size of a file with os.path.getsize but it prints out the size as bytes and I want to print it out in MB, any solutions? The code:

import os
from os import system
os = os.path.getsize("C:\\Users\\username\\Desktop\\ambrosial.exe")
print(str(os))
toolic
  • 57,801
  • 17
  • 75
  • 117
Krys
  • 1
  • 1
  • 2
    Yes. Divide by 1024*1024. Or divide by `1 << 20`. That aside, did you notice you’ve overwritten the `os` import as a variable? That aside, you’ve already imported `os`, so the second import is not needed. – S3DEV Apr 25 '21 at 15:22
  • 1
    Does this answer your question? [Better way to convert file sizes in Python](https://stackoverflow.com/questions/5194057/better-way-to-convert-file-sizes-in-python) – Tomerikoo Apr 25 '21 at 15:39

1 Answers1

1

Try this: Basically, you get the size of your file, then you convert the size of the file from bytes to megabytes by dividing by (1024 * 1024).

import os 
byte_size = os.path.getsize("C:\\Users\\username\\Desktop\\ambrosial.exe")
mb_size = byte_size / (1024 * 1024)
print(mb_size)
gabzo
  • 198
  • 1
  • 13