折腾:
期间,需要用Python去获取:
文件的大小
文件夹的整个的大小
python get file folder size
【总结】
最后用代码:import os
def getFileFolderSize(fileOrFolderPath):
"""get size for file or folder"""
totalSize = 0
if not os.path.exists(fileOrFolderPath):
return totalSize
if os.path.isfile(fileOrFolderPath):
totalSize = os.path.getsize(fileOrFolderPath) # 5041481
return totalSize
if os.path.isdir(fileOrFolderPath):
with os.scandir(fileOrFolderPath) as dirEntryList:
for curSubEntry in dirEntryList:
curSubEntryFullPath = os.path.join(fileOrFolderPath, curSubEntry.name)
if curSubEntry.is_dir():
curSubFolderSize = getFileFolderSize(curSubEntryFullPath) # 5800007
totalSize += curSubFolderSize
elif curSubEntry.is_file():
curSubFileSize = os.path.getsize(curSubEntryFullPath) # 1891
totalSize += curSubFileSize
return totalSize
def testNormalFile():
normalFile = "/Users/mac/working/dev_root/xxx/output/user/3946/show/57589460/show_57589460_video.mp4"
normalFileSize = getFileFolderSize(normalFile)
print("normalFileSize=%s" % normalFileSize)
def testFoler():
# userFolder = "/Users/mac/working/dev_root/xxx/output/user/3946"
userFolder = "/Users/mac/working/dev_root/xxx/output/course"
userFolderSize = getFileFolderSize(userFolder)
print("userFolderSize=%s" % userFolderSize) # userFolderSize=205849009
if __name__ == "__main__":
# testNormalFile()
testFoler()
可以实现:传入file或folder,都可以计算出大小。
注:最新代码已整理至: