To gauge the space occupancy of a directory, Python offers several approaches. Below we delve into an efficient and comprehensive solution:
<code class="python">import os def directory_size(start_path): total_size = 0 for root, directories, files in os.walk(start_path): for file in files: file_path = os.path.join(root, file) if not os.path.islink(file_path): total_size += os.path.getsize(file_path) return total_size</code>
The above is the detailed content of How to Efficiently Calculate Directory Size in Python?. For more information, please follow other related articles on the PHP Chinese website!