python读取大文件目录_【已解决】Python中获取文件大小和文件夹大小

折腾:

期间,需要用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,都可以计算出大小。

注:最新代码已整理至:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值