递归查找具有给定扩展名的最大文件的 Python 脚本
此简单 Python 脚本将递归列出给定目录中按文件大小排序的所有文件。你必须提供 -e/--extension 参数以仅考虑特定文件名扩展名。文件名扩展名以不区分大小写的方式比较。
list_largest_files.py
#!/usr/bin/env python3
import os
import argparse
def convert_size(size_bytes):
""" Convert byte size to a human-readable format. """
for unit in ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']:
if size_bytes < 1024:
return f"{size_bytes:.2f}{unit}"
size_bytes /= 1024
return f"{size_bytes:.2f}YB"
def get_files_with_extensions(root_dir, extensions):
""" Recursively find files with the given extensions in the directory. """
for root, dirs, files in os.walk(root_dir):
for file in files:
if any(file.lower().endswith(ext.lower()) for ext in extensions):
full_path = os.path.join(root, file)
size = os.path.getsize(full_path)
yield full_path, size
def main():
parser = argparse.ArgumentParser(description="List files with specific extensions sorted by size.")
parser.add_argument("directory", type=str, help="Directory to search")
parser.add_argument("-e", "--extension", type=str, action='append', help="File extension to filter by", required=True)
args = parser.parse_args()
files = list(get_files_with_extensions(args.directory, args.extension))
files.sort(key=lambda x: x[1])
for file, size in files:
print(f"{convert_size(size)} - {file}")
if __name__ == "__main__":
main()使用示例:查找最大的 Python 文件:
list_largest_py.sh
python ListLargestFiles.py -e py .使用示例:查找(某些类型的)电影:
list_largest_movies.sh
python ListLargestFiles.py -e mov -e avi -e mkv -e mp4 ~/Nextcloud示例输出:
list_largest_example_output.txt
[...]
108.81MB - /home/uli/Nextcloud/Google Fotos/20220925_151542.mp4
117.92MB - /home/uli/Nextcloud/Google Fotos/20220925_151958.mp4
237.51MB - /home/uli/Nextcloud/Google Fotos/20220911_115209.mp4
265.02MB - /home/uli/Nextcloud/Google Fotos/20220905_151716.mp4
317.36MB - /home/uli/Nextcloud/Google Fotos/20220912_124906.mp4
431.06MB - /home/uli/Nextcloud/Google Fotos/20220921_153051.mp4Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow