如何得到焦油的大小。python中的gz (MB)文件

时间:2022-05-28 21:07:58

I am doing backups in python script but i need to get the size of tar.gz file created in MB

我正在用python脚本进行备份,但是我需要得到tar的大小。以MB为单位创建的gz文件。

How can i get the size in MB of that file

如何获得该文件的MB大小

3 个解决方案

#1


26  

It's not clear from your question whether you want to the compressed or uncompressed size of the file, but in the former case, it's easy with the os.path.getsize function from the os module

您的问题并不清楚您想要文件的压缩或未压缩大小,但是在前一种情况下,使用os.path很容易。getsize函数来自os模块

>>> import os
>>> os.path.getsize('flickrapi-1.2.tar.gz')
35382L

To get the answer in megabytes you can shift the answer right by 20, e.g.

要以兆字节的速度得到答案,你可以在20秒内将答案右移。

os.path.getsize('large.tar.gz') >> 20

Although that operation will be done in integers - if you want to preserve fractions of a megabyte, divide by (1024*1024.0) instead. (Note the .0 so that the divisor will be a float.)

尽管该操作将以整数形式进行——如果您希望保留兆字节的部分,那么可以除以(1024*1024.0)。(注意0,使除数为浮点数)

Update: In the comments below, Johnsyweb points out a useful recipe for more generally producing human readable representations of file sizes.

更新:在下面的评论中,Johnsyweb指出了一个有用的方法,用于更广泛地生成人类可读的文件大小表示。

#2


2  

Use the os.stat() function to get a stat structure. The st_size attribute of that is the size of the file in bytes.

使用os.stat()函数获得一个stat结构。它的st_size属性是文件的大小(以字节为单位)。

#3


0  

According to Python on-line docs, you should look at the size attributes of a TarInfo object.

根据Python在线文档,您应该查看TarInfo对象的大小属性。

#1


26  

It's not clear from your question whether you want to the compressed or uncompressed size of the file, but in the former case, it's easy with the os.path.getsize function from the os module

您的问题并不清楚您想要文件的压缩或未压缩大小,但是在前一种情况下,使用os.path很容易。getsize函数来自os模块

>>> import os
>>> os.path.getsize('flickrapi-1.2.tar.gz')
35382L

To get the answer in megabytes you can shift the answer right by 20, e.g.

要以兆字节的速度得到答案,你可以在20秒内将答案右移。

os.path.getsize('large.tar.gz') >> 20

Although that operation will be done in integers - if you want to preserve fractions of a megabyte, divide by (1024*1024.0) instead. (Note the .0 so that the divisor will be a float.)

尽管该操作将以整数形式进行——如果您希望保留兆字节的部分,那么可以除以(1024*1024.0)。(注意0,使除数为浮点数)

Update: In the comments below, Johnsyweb points out a useful recipe for more generally producing human readable representations of file sizes.

更新:在下面的评论中,Johnsyweb指出了一个有用的方法,用于更广泛地生成人类可读的文件大小表示。

#2


2  

Use the os.stat() function to get a stat structure. The st_size attribute of that is the size of the file in bytes.

使用os.stat()函数获得一个stat结构。它的st_size属性是文件的大小(以字节为单位)。

#3


0  

According to Python on-line docs, you should look at the size attributes of a TarInfo object.

根据Python在线文档,您应该查看TarInfo对象的大小属性。