Home > Article > Backend Development > 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>
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>
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>
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!