如何使用python在许多文本文件中删除以“ 1”开头的行

我的目录中至少有3700个文本文件(实际上是标签),以及相同数量的jpg / jpeg图像。 在所有这些文本文件中,数百个文本为:

1 0.19140625 0.50078125 0.3078125 0.9484375

我想删除其中存在的每个文本文件中从1开始的行。 我尝试了以下操作:

import os
import glob
import errno
path = '~/Documents/txt/*.txt'
path1 = '~/Documents/txt/'
files = glob.glob(path)
txtfile = []
temp_path = os.path.join(path1,'temp.txt')
for name in files:
    try:
        with open(name,'r') as f,open(temp_path) as temp:
            for line in f:
                if line.strip() == "1":
                    continue
                temp.write(line)

    except IOError as exc:
        if exc.errno != errno.EISDIR:
            raise
#print(txtfile)
shuang890802 回答:如何使用python在许多文本文件中删除以“ 1”开头的行

import glob
import os

path = os.path.dirname(os.path.abspath(__file__))
for name in glob.glob(path+'/*.txt'):
    raw_data = open(name,'r')
    data_list = raw_data.read().splitlines()
    with open(name,"w") as new_file:
        for row in data_list:
            if row.startswith("1") is False:
                new_file.write(row + '\n')
,

我想出了以下几点:

path = '/home/mzaid/Documents/txt/*.txt'
for name in glob.glob(path):
    try:
        with open(name,'r+') as fl:
            for line in fl:
                if line.startswith("1"):
                    #not sure what to do here
    except IOError as exc:
        if exc.errno != errno.EISDIR:
            raise
本文链接:https://www.f2er.com/2666346.html

大家都在问