简体   繁体   English

Python 中的磁盘/分区大小不同于 linux 命令

[英]Size of disk/partition in Python is different than linux command

Using: Python 3.7.6, OS Linux (rmp)使用: Python 3.7.6,操作系统 Linux (rmp)

I need to extract total and free sizes of disks and partitions in Python.我需要提取 Python 中磁盘和分区的总大小和可用大小。

I tried different Python functions to retrieve block size in bytes (the results were the same).我尝试了不同的 Python 函数来检索以字节为单位的块大小(结果相同)。 Then I compared to results of Linux commands (also in bytes), results were different (Linux commands different than Python).然后我比较了 Linux 命令的结果(也以字节为单位),结果不同(Linux 命令不同于 Python)。

Python functions: Python 功能:

  1. Using os.statvfs使用os.statvfs
import os 
statvfs  = os.statvfs('/dev/sda')     
statvfs.f_frsize *statvfs.f_blocks   
statvfs.f_frsize * statvfs.f_bfree

Results:
size of /dev/sda is 1973968896
size of /dev/sda1 is 1973968896 (the same??!)
  1. Using shutil.disk_usage :使用shutil.disk_usage
print(shutil.disk_usage('/dev/sda'))
usage(total=1973968896, used=0, free=1973968896)

print(shutil.disk_usage('/dev/sda1') )
usage(total=1973968896, used=0, free=1973968896)

I tried also psutil :我也试过psutil

import psutil
print(psutil.disk_usage('/dev/sda'))
print(psutil.disk_usage('/dev/sda1'))
same results as 1. and 2.

Linux command: Linux 命令:

  1. blockdev --getsize64 #Print device size in bytes blockdev --getsize64 #以字节为单位打印设备大小
blockdev --getsize64 /dev/sda
8001563222016

blockdev --getsize64 /dev/sda1
80014737408


  1. lsblk -b lsblk -b

在此处输入图像描述

What do I miss here?我在这里想念什么? Why Python results are different than Linux command (while all units in bytes)?为什么 Python 结果与 Linux 命令不同(而所有单位都以字节为单位)? Why Python sizes of sda and sda1 are the same?为什么 sda 和 sda1 的 Python 大小相同?

Bothhttps://docs.python.org/3/library/shutil.html#shutil.disk_usage and https://docs.python.org/3/library/os.html#os.statvfs are used to get stats on filesystem and not a block device, so when you are using /dev/sda or /dev/sda1 as argument - you are actually getting info about /dev Bothhttps://docs.python.org/3/library/shutil.html#shutil.disk_usage and https://docs.python.org/3/library/os.html#os.statvfs are used to get stats on文件系统而不是块设备,因此当您使用/dev/sda/dev/sda1作为参数时 - 您实际上正在获取有关/dev的信息

You can get similar results with stat command, here is example with my /dev/sdb .您可以使用stat命令获得类似的结果,这里是我的/dev/sdb示例。

In python:在 python 中:

print(shutil.disk_usage('/dev/sdb'))                                                                                                                                                
usage(total=8324083712, used=0, free=8324083712)

print(shutil.disk_usage('/dev'))                                                                                                                                                
usage(total=8324083712, used=0, free=8324083712)

In shell:在 shell 中:

LANG=C stat -f /dev/sdb   
  File: "/dev/sdb"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 2032247    Free: 2032247    Available: 2032247
Inodes: Total: 2032247    Free: 2031625

LANG=C stat -f /dev/   
  File: "/dev/"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 2032247    Free: 2032247    Available: 2032247
Inodes: Total: 2032247    Free: 2031625

echo $(( 2032247 * 4096 ))
8324083712

You can see that shutil output and block size * blocks total are exactly the same.可以看到shutil output和block size * blocks total是完全一样的。

Notice the -f flag:注意 -f 标志:

-f, --file-system display file system status instead of file status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM