如何在Python中检查文件大小?

问题描述 投票:0回答:11

如何在Python中获取文件的大小?

python file
11个回答
1533
投票

使用

os.path.getsize

>>> import os
>>> os.path.getsize("/path/to/file.mp3")
2071611

输出以字节为单位。


1081
投票

您需要 st_size

 返回的对象的 os.stat
 属性。您可以使用 
pathlib
(Python 3.4+)获取它:

>>> from pathlib import Path >>> Path('somefile.txt').stat() os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400) >>> Path('somefile.txt').stat().st_size 1564
或使用 

os.stat

:

>>> import os >>> os.stat('somefile.txt') os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400) >>> os.stat('somefile.txt').st_size 1564
输出以字节为单位。


152
投票
其他答案适用于真实文件,但如果您需要适用于“类似文件对象”的东西(例如

StringIO

),请尝试以下操作:

# f is a file-like object. old_file_position = f.tell() size = f.seek(0, os.SEEK_END) f.seek(old_file_position)

注意事项#1:

“类文件对象”API 不是严格的接口,但

API 文档 建议类文件对象应该 支持 seek()

tell()
,但是

注意事项#2:

这假设当前文件位置位于文件的开头。例如。如果您已经从 M 字节文件中读取了 N 个字节,则此技术会将大小报告为 (M-N)。如果您根本不关心文件位置,您可以将上面的 3 行简化为

size = f.seek(0, os.SEEK_END)

注意事项#3:

这与

os.stat()

 之间的一个重要区别是,即使您没有读取文件的权限,您也可以 
stat()
 文件。显然,除非您有阅读权限,否则 
seek()
 方法将不起作用。


110
投票
import os def convert_bytes(num): """ this function will convert bytes to MB.... GB... etc """ for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: if num < 1024.0: return "%3.1f %s" % (num, x) num /= 1024.0 def file_size(file_path): """ this function will return the file size """ if os.path.isfile(file_path): file_info = os.stat(file_path) return convert_bytes(file_info.st_size) # Lets check the file size of MS Paint exe # or you can use any file path file_path = r"C:\Windows\System32\mspaint.exe" print file_size(file_path)
结果:

6.1 MB
    

68
投票
使用

pathlib

在Python 3.4中添加或PyPI上可用的向后移植):

from pathlib import Path file = Path() / 'doc.txt' # or Path('./doc.txt') size = file.stat().st_size
这实际上只是 

os.stat

 周围的一个界面,但是使用 
pathlib
 提供了一种访问其他文件相关操作的简单方法。


30
投票
如果我想从

bitshift

 转换为任何其他单位,我可以使用 
bytes
 技巧。如果你右移
10
,你基本上就将它移动了一个订单(多个)。 

示例:

5GB are 5368709120 bytes


print (5368709120 >> 10) # 5242880 kilobytes (kB) print (5368709120 >> 20 ) # 5120 megabytes (MB) print (5368709120 >> 30 ) # 5 gigabytes (GB)
    

18
投票
我们有两个选项都包括导入操作系统模块

1)

import os os.stat("/path/to/file").st_size
as 

os.stat()

函数返回一个对象,其中包含许多标头,包括文件创建时间和上次修改时间等。其中
st_size
给出了文件的确切大小。
文件路径可以是绝对路径或相对路径。

2) 在此,我们必须提供准确的文件路径,文件路径可以是相对路径或绝对路径。

import os os.path.getsize("path of file")
    

9
投票
严格抓住问题,Python 代码(+伪代码)将是:

import os file_path = r"<path to your file>" if os.stat(file_path).st_size > 0: <send an email to somebody> else: <continue to other things>
    

0
投票
您可以使用

stat()

 模块中的 
os
 方法。您可以为它提供字符串、字节甚至 PathLike 对象形式的路径。它也适用于文件描述符。

import os res = os.stat(filename) res.st_size # this variable contains the size of the file in bytes
    

0
投票
这是另一个不言自明的例子。这样,字节将自动转换为 MB、GB 或 TB。

from pathlib import Path from psutil._common import bytes2human def get_readable_filesize(text_file: Path): return bytes2human(text_file.stat().st_size) if __name__ == '__main__': current_file = Path(__file__).parent.resolve() print(get_readable_filesize(current_file / 'file.txt'))
    

-3
投票
#Get file size , print it , process it... #Os.stat will provide the file size in (.st_size) property. #The file size will be shown in bytes. import os fsize=os.stat('filepath') print('size:' + fsize.st_size.__str__()) #check if the file size is less than 10 MB if fsize.st_size < 10000000: process it ....
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.