首页 > 编程语言 >How to get file size in Python? 获取文件大小Python

How to get file size in Python? 获取文件大小Python

时间:2022-12-02 00:23:19浏览次数:83  
标签:文件大小 get Python bytes cursor file os size

How to get file size in Python?

We can follow different approaches to get the file size in Python. It’s important to get the file size in Python to monitor file size or in case of ordering files in the directory according to file size. 

Method 1: Using getsize function of os.path module

This function takes a file path as an argument and it returns the file size (bytes). 

Example:

 

# approach 1
# using getsize function os.path module
import os
 
file_size = os.path.getsize('d:/file.jpg')
print("File Size is :", file_size, "bytes")

 

Method 2: Using stat function of the OS module

This function takes a file path as an argument (string or file object) and returns statistical details about file path given as input. 

Example:

# approach 2
# using stat function of os module
import os
 
file_size = os.stat('d:/file.jpg')
print("Size of file :", file_size.st_size, "bytes")

 

Method 3: Using File Object

To get the file size, follow these steps –

  1. Use the open function to open the file and store the returned object in a variable.  When the file is opened, the cursor points to the beginning of the file.
  2. File object has seek method used to set the cursor to the desired location. It accepts 2 arguments – start location and end location. To set the cursor at the end location of the file use method os.SEEK_END.
  3. File object has tell method that can be used to get the current cursor location which will be equivalent to the number of bytes cursor has moved. So this method actually returns the size of the file in bytes.
# approach 3
# using file object
 
# open file
file = open('d:/file.jpg')
 
# get the cursor positioned at end
file.seek(0, os.SEEK_END)
 
# get the current position of cursor
# this will be equivalent to size of file
print("Size of file is :", file.tell(), "bytes")

 

Method 4: Using Pathlib Module

The stat() method of the Path object returns st_mode, st_dev, etc. properties of a file. And, st_size attribute of the stat method gives the file size in bytes.

# approach 4
# using pathlib module
from pathlib import Path
 
# open file
Path(r'd:/file.jpg').stat()
 
# getting file size
file=Path(r'd:/file.jpg').stat().st_size
 
# display the size of the file
print("Size of file is :", file, "bytes")
 
# this code was contributed by debrc

 

标签:文件大小,get,Python,bytes,cursor,file,os,size
From: https://www.cnblogs.com/chucklu/p/16943225.html

相关文章

  • 今天很开心,get了3个知识点
    1、首先搞懂了bindtap和bindinput的区别;bindinput在input标签里面,输入框的每次输入都会触发bindinput绑定的方法;bindtap在其他标签,比如button。button的每次点击都会出发......
  • 7.2 图像文件压缩。使用PIL库对图像进行等比例压缩,无论压缩前文件大小如何,压缩后文件
    python实现图像文件等比例压缩 使用Image类中的open()方法从文件加载图像,使用其中的size属性获得图像的宽度和高度。os模块中的path.getsize()方法可以获得文件的大小,......
  • linux使用wget命令批量下载rpm包
    下载命令:wget-nd-r-l1-A.rpm--no-parenthttp://mirrors.163.com/centos/7/os/x86_64/Packages/-erobots=off下载时,仅需要把以上链接换成要下载rpm包路......
  • Python13-实战
    实战01(模拟篮球自动弹跳)#-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygamesize=width,height=640,......
  • python第13章实例
    #_*_coding:utf-8_*_importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(0,0,0)ball=pygame.ima......
  • Python第十三章小球移动游戏
    #-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygamesize=width,height=640,480#设置窗口screen=pyga......
  • Python第十三章实验报告
    第十三章实验报告——篮球自动弹跳代码如下:1#----------实例01:制作一个跳跃的小球游戏----------#2importsys3importpygame4pygame.init()......
  • python篮球自动弹跳
           具体思路是首先导入sys和pygame模块然后初始化pygame然后显示窗口加载篮球图片执行死循环检查事件设置移动篮球将图片画在窗口上最后更新全部显示......
  • python-练习(知识点到逻辑运算符)
    1.在终端中显示古诗"登高"print("登高")print("作者:杜甫")print("风急天高猿啸哀,渚清沙白鸟飞回。")print("无边落木萧萧下,不尽长江滚滚来。")pr......
  • python第十三章实例1
    #-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块p......