How to get the size of file in python?

Python文件大小查询
本文介绍如何使用Python的os模块来获取文件的大小。通过os.stat()函数可以得到文件的状态信息,其中包括文件的大小。示例代码展示了如何获取'somefile.txt'及'temp'文件的大小。

I must learn to use google to solve problems. Input "python get file size" in google.

  solution:

command line: input python, into:

      >>> import os

      >>> statinfo = os.stat('somefile.txt')  # filename

      >>> statinfo

          (33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)

      >>> statinfo.st_size

          926L    # size of somefile.txt

 

 

input: os.stat ("temp"),

posix.stat_result(st_mode=33188, st_ino=279987L, st_dev=2049L, st_nlink=1, st_uid=1000, st_gid=1000, st_size=1316889L, st_atime=1340701004, st_mtime=1340701837, st_ctime=1340701837)

>>> sinfo = os.stat("temp")

>>> output("Length: %s" % sinfo.st_size)    # size of temp

>>>import os
>>> statinfo = os.stat('somefile.txt')
>>> statinfo
(33188,422511L,769L,1,1032,100,926L,1105022698,1105022732,1105022732)
>>> statinfo.st_size
926L

转载于:https://www.cnblogs.com/nlpowen/archive/2012/06/26/2564091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值