我正在尝试使用 os.path.getsize 获取文件的大小,但它以字节为单位打印出大小,我想以 MB 为单位打印出来,有什么解决方案吗?

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?

我正在尝试使用 os.path.getsize 获取文件的大小,但它以字节为单位打印出大小,我想以 MB 为单位打印出来,有什么解决方案吗? 代码:

import os
from os import system
os = os.path.getsize("C:\Users\username\Desktop\ambrosial.exe")
print(str(os))

试试这个: 基本上,您得到文件的大小,然后通过除以 (1024 * 1024) 将文件的大小从字节转换为兆字节。

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