Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果PYTHON第4行中没有记录,则删除文件_Python_Bioinformatics - Fatal编程技术网

如果PYTHON第4行中没有记录,则删除文件

如果PYTHON第4行中没有记录,则删除文件,python,bioinformatics,Python,Bioinformatics,我有一组扩展名为“.tab”的文件,这些文件是由生物信息学中的一个工具生成的,该工具正在研究一个在细菌中发现的防御系统,称为BREX。 文件的文件格式如下所示: # --- full sequence --- -------------- this domain ------------- hmm coord ali coord env

我有一组扩展名为“.tab”的文件,这些文件是由生物信息学中的一个工具生成的,该工具正在研究一个在细菌中发现的防御系统,称为BREX。 文件的文件格式如下所示:

#                                                                            --- full sequence --- -------------- this domain -------------   hmm coord   ali coord   env coord
# target name        accession   tlen query name           accession   qlen   E-value  score  bias   #  of  c-Evalue  i-Evalue  score  bias  from    to  from    to  from    to  acc description of target
#------------------- ---------- ----- -------------------- ---------- ----- --------- ------ ----- --- --- --------- --------- ------ ----- ----- ----- ----- ----- ----- ----- ---- ---------------------
#
# Program:         hmmsearch

在这种情况下,文件没有记录,因为在第4行我们没有记录。如果文件有记录,则第四行的记录不会为空。我的任务是删除这些没有记录的文件,这些“空”文件(不是完全空的,因为它们写了一些东西)在我有所有“.tab”文件集的文件夹中。对于此任务,我尝试执行以下代码:

def deleteEmptyFiles (brexType):
    BREXFolder = "./"
    allfiles = os.listdir(BREXFolder)
    allBREXfiles = []
    for x in allfiles:
        if x.endswith("%s.tab" %brexType):
            allBREXfiles.append(x)
    for x in allBREXfiles:
        with open(x,"r") as f:
                for line in f:
                    lineNo = 1
                    while lineNo < 5:
                        lineNo +=1
                    i=1
                    for i in range(lineNo):
                        if not line.strip('#'):
                            os.remove(x)
                            print("File %s is removed! It has no records." %x)
def deleteEmptyFiles(brexType):
BREXFolder=“./”
allfiles=os.listdir(BREXFolder)
AllBrexFile=[]
对于所有文件中的x:
如果x.endswith(“%s.tab”%brexType):
allBREXfiles.append(x)
对于所有BREXFILES中的x:
开放式(x,“r”)为f:
对于f中的行:
lineNo=1
而lineNo<5:
lineNo+=1
i=1
对于范围内的i(行号):
如果不是行。条带(“#”):
删除操作系统(x)
打印(“文件%s已删除!它没有记录。”%x)
我的逻辑是找出4号线是否有记录。如果没有,我想删除该文件。我执行程序,但它什么也不做。
请帮忙

我不知道您的文件的确切结构,但我设法做到了以下几点:

import os

def deleteEmptyFiles():
    BREXFolder = "./"
    for fh in os.listdir(BREXFolder):
        if fh.endswith('.tab'):
            opened_file = open(os.path.join(BREXFolder, fh), 'r')
            data = [line.strip() for line in opened_file]
            if data[3] == '#':
                opened_file.close()
                print(f'Deleted file {fh}')
                os.remove(fh)
还有更有效的方法。不必读取整个文件,您只需遍历它,并比较行号和它的数据。给你:

import os

def deleteEmptyFiles():
    BREXFolder = "./"
    for fh in os.listdir(BREXFolder):
        if fh.endswith('.tab'):
            opened_file = open(os.path.join(BREXFolder, fh), 'r')
            for i, line in enumerate(opened_file, 1):
                if i == 4 and line.strip() == '#':
                    opened_file.close()
                    print(f'Removed file {fh}')
                    os.remove(fh)
                    break

if __name__ == '__main__':
    deleteEmptyFiles()

我不知道您的文件的确切结构,但我设法做到了以下几点:

import os

def deleteEmptyFiles():
    BREXFolder = "./"
    for fh in os.listdir(BREXFolder):
        if fh.endswith('.tab'):
            opened_file = open(os.path.join(BREXFolder, fh), 'r')
            data = [line.strip() for line in opened_file]
            if data[3] == '#':
                opened_file.close()
                print(f'Deleted file {fh}')
                os.remove(fh)
还有更有效的方法。不必读取整个文件,您只需遍历它,并比较行号和它的数据。给你:

import os

def deleteEmptyFiles():
    BREXFolder = "./"
    for fh in os.listdir(BREXFolder):
        if fh.endswith('.tab'):
            opened_file = open(os.path.join(BREXFolder, fh), 'r')
            for i, line in enumerate(opened_file, 1):
                if i == 4 and line.strip() == '#':
                    opened_file.close()
                    print(f'Removed file {fh}')
                    os.remove(fh)
                    break

if __name__ == '__main__':
    deleteEmptyFiles()

既然这些文件与有记录的文件相比必须非常小,你就不能检查每个文件的大小并删除那些与“空”文件匹配的文件吗?既然这些文件与有记录的文件相比必须非常小,你就不能检查每个文件的大小并删除那些与“空”文件匹配的文件吗?