如何使用Python代码列出指定目录中所有大小超过5MB的文件?
时间: 2024-10-25 13:09:07 浏览: 11
在Python中,你可以使用`os`模块和`shutil`模块来列出指定目录中所有大于5MB的文件。首先,你需要导入必要的库,然后遍历目录并检查每个文件的大小。下面是一个简单的示例:
```python
import os
import shutil
def list_large_files(directory, size_threshold=5 * 1024 * 1024): # 5MB = 5 * 1 MB
large_files = []
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
if os.path.getsize(file_path) > size_threshold:
large_files.append(file_path)
return large_files
# 使用函数
directory_to_search = '/path/to/your/directory'
large_files_list = list_large_files(directory_to_search)
print("Large files larger than 5MB in directory:", large_files_list)
阅读全文