检查E://All2中所有子文件夹中的tif文件并删除
时间: 2024-10-14 18:17:49 浏览: 25
如果你想在E盘"All2"目录及其所有子文件夹中查找并删除所有的.tif文件,可以使用类似上面提到的Python代码,只需将目录路径替换为"E:\\All2"。以下是完整的代码段:
```python
import os
import shutil
directory = 'E:\\All2'
def remove_tif_files_in_subdirectories(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.tif'):
file_path = os.path.join(root, file)
try:
os.remove(file_path)
print(f"Deleted: {file_path}")
except PermissionError:
print(f"Permission denied while deleting {file_path}. Please close any programs that might be using these files.")
except Exception as e:
print(f"Error deleting {file_path}: {e}")
remove_tif_files_in_subdirectories(directory)
```
这段代码会遍历所有子目录,并尝试删除所有的.tif文件。如果遇到权限问题,它会打印错误信息而不是强行删除。
阅读全文