yolo_standard_libray/015查看一个文件夹是否为空.py
2025-03-07 11:35:40 +08:00

22 lines
874 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
def is_file_empty(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
return not file.read().strip()
except FileNotFoundError:
# 如果文件不存在可以视为“空”或抛出异常这里选择返回True
return True
if __name__ == '__main__':
# txt_path = '/home/admin-root/haotian/python哈汽安全帽识别/fire_smoke/train/labels'
txt_path = '/home/admin-root/haotian/python哈汽安全帽识别/fire_smoke/val/labels'
# save_path = 'fire_smoke/remove/train.txt'
save_path = 'fire_smoke/remove/val.txt'
all_txt_path = [os.path.join(txt_path, t) for t in os.listdir(txt_path)]
for txt in all_txt_path:
if is_file_empty(txt):
with open(save_path, 'a') as f:
f.writelines(txt+'\n')
# print(txt)