python判断txt是否为空,并跳过空文件

news/2024/7/24 5:28:50 标签: python

问题:对批量txt文件做一些读写操作,一遇到为空的文件使得程序报错并终止,怎么判断一个txt文件内容是否为空,为空的话怎么跳过这些文件以继续下面的操作,并将为空的txt文件夹名称打印输出呢?
解决思路:
1、os.path.getsize() 返回文件的字节数,如果为 0,则代表空。
2、continue继续下面的操作

代码展示:`

python">import os
for filename_1 in os.listdir(r"G:/Deep_Code/yolov4-pytorch-master/map_out/detection-results/"): 
     print(filename_1)
     #for txt_name in filename_1.readlines():
     #txt_name  = txt_name.strip('\n')
     #print(txt_name)
     filename_1 = filename_1.strip('/n')
     #遍历detection-results里txt的路径
     detection_results_txt_path  = os.path.join('G:/Deep_Code/yolov4-pytorch-master/map_out/detection-results/', filename_1)
     #打开txt
     with open (detection_results_txt_path) as txt_1:
         #遍历txt的每一行
         #新建一个列表,将每个目标的像素面积放入到该列表当中
         if not os.path.getsize(detection_results_txt_path):
             print(filename_1, " is empty!")
             continue

增加判断的内容代码为:

python">if not os.path.getsize(detection_results_txt_path):
             print(filename_1, " is empty!")
             continue

之后程序成功执行完毕。

拓展:
1、将判断为空的文件夹删除,并继续

python">import os
 
for filename_1 in os.listdir(r"G:/Deep_Code/yolov4-pytorch-master/map_out/detection-results/"): 
     print(filename_1)
     #for txt_name in filename_1.readlines():
     #txt_name  = txt_name.strip('\n')
     #print(txt_name)
     filename_1 = filename_1.strip('/n')
     #遍历detection-results里txt的路径
     detection_results_txt_path  = os.path.join('G:/Deep_Code/yolov4-pytorch-master/map_out/detection-results/', filename_1)
     #打开txt
     with open (detection_results_txt_path) as txt_1:
         #遍历txt的每一行
         #新建一个列表,将每个目标的像素面积放入到该列表当中
         if not os.path.getsize(detection_results_txt_path):
             print(filename_1, " is empty!")
             os.remove(filename_1)
             continue

2、判断文件是否存在
os.path.exists() 方法用于检验文件 / 文件夹是否存在。

python">import os
path = "/home/wzh/predict_dir"
file = "/home/wzh/predict_file"
 
if not os.path.exists(path):
    os.path.makedirs(path)
 
if not os.path.exists(file):
    pass

3、先判断文件是否存在,如果存在则判断是否为空:

python"># 文件是否存在,以及是否为空
file = "/home/wzh/predict_file.txt"
if os.path.exists(file):
    print(file, " is exists!")
    sz = os.path.getsize(file)
    if not sz:
        print(file, " is empty!")
    else:
        print(file, " is not empty, size is ", sz)
else:
    print(file, " is not exists!")

参考博客链接:https://blog.csdn.net/sinat_26811377/article/details/107368659


http://www.niftyadmin.cn/n/1726042.html

相关文章

NOI模拟:保镖(Hall定理)

题意: 给一个二分图,找出满足以下条件的点集的个数: 1.该点集是原点集的子集。 2.该点集是一个匹配的点集的子集。 (n ≤ 20) 题解: 首先任意匹配的点集一定是一个最大匹配的子集。 题意转化为找出是…

批量更改图片名称、size大小

代码 import os import time from PIL import Imagedef alter(path,object):s os.listdir(path) #改尺寸像素大小代码段 """ count 1for i in s:document os.path.join(path,i)img Image.open(document)out img.resize((3840,2160))listStr [str(int(tim…

BZOJ3924: [Zjoi2015]幻想乡战略游戏(动态树分治)

传送门 题意: 给一棵树,每次给一些点增加点权,求一个点,使得∑j≠idis(i,j)∗val[j]最小,输出最小值。 题解:动态树分治 官方题解已经讲得很清楚了: 首先我们假设每次操作过后我们可以快速地在线查询以…

将图像按7:3划分train和val

问题:如何将原图按7:3划分train和val两个分类集呢? 代码: # -*- coding:utf-8 -*- #author :wu # 将一个文件夹下图片按比例分在两个文件夹下,比例改0.7这个值即可 import os import random import shutil from shutil import co…

BZOJ3676: [Apio2014]回文串(后缀自动机+manacher/回文自动机)

传送门 题意: 给一个字符串s。定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度。求出s的所有回文子串中的最 大出现值。 题解: manacher求出所有本质不同的回文子串后在后缀自动机上二分。 (网上说的什么回文自动机感觉…

BZOJ1030: [JSOI2007]文本生成器(AC自动机)

传送门 题意: 给n个串,和字符串s的长度t。求满足包含至少一个串的字符串s的个数。 题解: 转化为求一个串都不满足的个数。可以直接在AC自动机上DP。 但注意比较坑的一点是如果一个串的子串中有不合法的子串的话也是不合法的。 #include…

dlib.get_frontal_face_detector()批量预测

问题:怎么使用dlib.get_frontal_face_detector()批量预测自己划分的验证数据集? 代码: #author:wu #导入需要的库 import cv2 import dlib import os import sys import random import numpy as np #------------没有该库则pip安…

poj3243:Clever Y(BSGS)

传送门 题意&#xff1a; 求Ax≡B(modC)的最小X解。 题解&#xff1a; BSGS 。 互质很好想。 对于不互质&#xff1a; #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<cstdio> using namespace s…