How Can I Calculate the Size of a Directory in Python?

DDD
Release: 2024-11-02 06:20:29
Original
266 people have browsed it

How Can I Calculate the Size of a Directory in Python?

Calculating Directory Size with Python: A Comprehensive Guide

When navigating through extensive file systems, determining a directory's size can become invaluable. Python offers a range of modules that encompass this functionality.

Consider the following Python code snippet:

<code class="python">import os

def get_size(start_path='.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            # Skip symbolic links
            if not os.path.islink(fp):
                total_size += os.path.getsize(fp)

    return total_size

print(get_size(), 'bytes')</code>
Copy after login

This solution provides a comprehensive approach by recursively traversing subdirectories, accumulating file sizes. It avoids symbolic links to ensure accurate size calculations. The formatted size (e.g., Mb/Gb) is not directly incorporated but can be easily added as a follow-on step.

Additionally, for calculating only the root directory's size without descending into subdirectories, a streamlined approach using os.listdir is available:

<code class="python">import os

sum(os.path.getsize(f) for f in os.listdir('.') if os.path.isfile(f))</code>
Copy after login

This code snippet compactly sums the file sizes within the current directory, excluding subdirectories.

Furthermore, for precise file size determination, os.path.getsize is preferred over os.stat().st_size. The latter can be misleading due to potential optimizations and overheads.

"os.scandir" has proven to be an efficient alternative to "os.walk" in Python 3.4 and below. And in Python 3.5 onwards, "os.walk" has been enhanced to match "os.scandir" performance.

For a more contemporary approach, "pathlib" offers a convenient solution:

<code class="python">from pathlib import Path

root_directory = Path('.')
sum(f.stat().st_size for f in root_directory.glob('**/*') if f.is_file())</code>
Copy after login

This code leverages the "glob" function for a recursive search and utilizes "Path" objects for enhanced file handling.

In summary, Python provides an arsenal of functions to cater to your directory size calculation needs. Choose the approach that best aligns with your requirements to efficiently navigate and manage your file systems.

The above is the detailed content of How Can I Calculate the Size of a Directory in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!