从python获取文件夹和所有文件大小的最佳方法是什么?

时间:2023-01-14 11:13:16

If there will be a small number of files it should be easy with a recursive function to pass through all the files and add the size but what if there are lots of files, and by lots i really mean lots of files.

如果将有少量文件,使用递归函数来传递所有文件并添加大小应该很容易,但如果有大量文件,那么如果有很多文件我会说很多文件。

4 个解决方案

#1


10  

You mean something like this?

你的意思是这样的?

import os
for path, dirs, files in os.walk( root ):
    for f in files:
        print path, f, os.path.getsize( os.path.join( path, f ) )

#2


5  

There is no other way to compute the size than recursively invoking stat. This is independent of Python; the operating system just provides no other way.

没有其他方法可以计算大小而不是递归调用stat。这与Python无关;操作系统只是提供其他方式。

The algorithm doesn't have to be recursive; you can use os.walk.

该算法不必是递归的;你可以使用os.walk。

There might be two exceptions to make it more efficient:

可能有两个例外,以提高效率:

  1. If all the files you want to measure fill a partition, and the partition has no other files, then you can look at the disk usage of the partition.
  2. 如果要测量的所有文件都填充分区,并且该分区没有其他文件,则可以查看该分区的磁盘使用情况。

  3. If you can continuously monitor all files, or are responsible for creating all the files yourself, you can generate an incremental disk usage.
  4. 如果您可以持续监视所有文件,或者负责自己创建所有文件,则可以生成增量磁盘使用情况。

#3


1  

There is a recipe for that problem in the Python Cookbook (O'Reilly). You can read the full solution with an example online:

Python Cookbook(O'Reilly)中有一个解决这个问题的方法。您可以通过在线示例阅读完整的解决方案:

http://safari.oreilly.com/0596001673/pythoncook-CHP-4-SECT-24

or here:

http://books.google.com/books?id=yhfdQgq8JF4C&pg=PA152&dq=du+command+in+python

#4


0  

If you are on a posix system that provides du, you should be able to use pexpect.run or subprocess to execute du <path to folder> to get the size of the folder and subfiles. Keep in mind it will output a string with each file listed and each folder totaled. (Look at the du manpage to see what you can do to limit that).

如果你在提供du的posix系统上,你应该能够使用pexpect.run或subprocess来执行du 来获取文件夹和子文件的大小。请记住,它将输出一个字符串,其中列出了每个文件,并且每个文件夹总计。 (查看du manpage以了解您可以做些什么来限制它)。

#1


10  

You mean something like this?

你的意思是这样的?

import os
for path, dirs, files in os.walk( root ):
    for f in files:
        print path, f, os.path.getsize( os.path.join( path, f ) )

#2


5  

There is no other way to compute the size than recursively invoking stat. This is independent of Python; the operating system just provides no other way.

没有其他方法可以计算大小而不是递归调用stat。这与Python无关;操作系统只是提供其他方式。

The algorithm doesn't have to be recursive; you can use os.walk.

该算法不必是递归的;你可以使用os.walk。

There might be two exceptions to make it more efficient:

可能有两个例外,以提高效率:

  1. If all the files you want to measure fill a partition, and the partition has no other files, then you can look at the disk usage of the partition.
  2. 如果要测量的所有文件都填充分区,并且该分区没有其他文件,则可以查看该分区的磁盘使用情况。

  3. If you can continuously monitor all files, or are responsible for creating all the files yourself, you can generate an incremental disk usage.
  4. 如果您可以持续监视所有文件,或者负责自己创建所有文件,则可以生成增量磁盘使用情况。

#3


1  

There is a recipe for that problem in the Python Cookbook (O'Reilly). You can read the full solution with an example online:

Python Cookbook(O'Reilly)中有一个解决这个问题的方法。您可以通过在线示例阅读完整的解决方案:

http://safari.oreilly.com/0596001673/pythoncook-CHP-4-SECT-24

or here:

http://books.google.com/books?id=yhfdQgq8JF4C&pg=PA152&dq=du+command+in+python

#4


0  

If you are on a posix system that provides du, you should be able to use pexpect.run or subprocess to execute du <path to folder> to get the size of the folder and subfiles. Keep in mind it will output a string with each file listed and each folder totaled. (Look at the du manpage to see what you can do to limit that).

如果你在提供du的posix系统上,你应该能够使用pexpect.run或subprocess来执行du 来获取文件夹和子文件的大小。请记住,它将输出一个字符串,其中列出了每个文件,并且每个文件夹总计。 (查看du manpage以了解您可以做些什么来限制它)。