import os
def get_FileSize(filePath):
fsize = os.path.getsize(filePath)
fsize = fsize / float(1024*1024)
return round(fsize, 2)
if __name__ == '__main__':
size = get_FileSize(r"E:\report\AI2_reverse\2021_12_09_17_42_24\\report.txt")
print("文件大小:%.2f MB" % (size))
运行结果:
D:\Python3.8.6\python.exe D:/PythonWorkSpace/chenbang/test_3.py
文件大小:0.02 MB
Process finished with exit code 0
1G=1024MB
1M=1024KB
1KB=1024B