检查文件是否具有相同的名称,以及同名文件的存储行计数

2024-09-19 23:35:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python比较陌生,我真的可以使用你们中的一些人的输入。在

我运行了一个脚本,该脚本以以下格式存储文件:

201309030700__81.28.236.2.txt
201308240115__80.247.17.26.txt
201308102356__84.246.88.20.txt
201309030700__92.243.23.21.txt
201308030150__203.143.64.11.txt

每个文件都有一些代码行,我想计算它的总数,然后我想存储这个。例如,我想浏览这些文件,如果一个文件有相同的日期(文件名的第一部分),那么我希望以以下格式将其存储在同一个文件中。在

^{pr2}$

创建一个日期为20130903的文件(最后4位是我不想要的时间)。创建文件: 20130903.txt 有两行代码 10 八

我有以下代码,但我什么也做不到,请帮忙。在

import os, os.path
asline = []
ipasline = []

def main():
    p = './results_1/'
    np = './new/'
    fd = os.listdir(p)
    run(fd)

def writeFile(fd, flines):
    fo = np+fd+'.txt'
    with open(fo, 'a') as f:    
        r = '%s\t %s\n' % (fd, flines)
        f.write(r)

def run(path):
    for root, dirs, files in os.walk(path):
       for cfile in files:
            stripFN = os.path.splitext(cfile)[0]
            fileDate = stripFN.split('_')[0]
            fileIP = stripFN.split('_')[-1]     
        if cfile.startswith(fileDate):
                hp = 0
                for currentFile in files.readlines()[1:]:
                    hp += 1
                    writeFile(fdate, hp)

我试着用这个剧本:

if not os.path.exists(os.path.join(p, y)):  
    os.mkdir(os.path.join(p, y))
    np = '%s%s' % (datetime.now().strftime(FORMAT), path)
if os.path.exists(os.path.join(p, m)):
    os.chdir(os.path.join(p, month, d))
    np = '%s%s' % (datetime.now().strftime(FORMAT), path)

其中FORMAT具有以下值

20130903

但我似乎没法让它发挥作用。在

编辑: 我修改了代码如下,它有点像我想做的,但可能我做了多余的事情,我仍然没有考虑到我正在处理大量的文件,所以这可能不是最有效的方法。请看一下。在

import re, os, os.path


p = './results_1/'
np = './new/'
fd = os.listdir(p)
star = "*"


def writeFile(fd, flines):
    fo = './new/'+fd+'_v4.txt'
    with open(fo, 'a') as f:    
    r = '%s\n' % (flines)
    f.write(r)

for f in fd:
    pathN = os.path.join(p, f)
    files = open(pathN, 'r')
    fileN = os.path.basename(pathN)
    stripFN = os.path.splitext(fileN)[0]
    fileDate = stripFN.split('_')[0]
    fdate = fileDate[0:8]
    lnum = len(files.readlines())
    writeFile(fdate, lnum)
    files.close()

此时,它正在向一个文件写入一个新的行数为文件计数的每一行。不过,我已经整理好了。非常感谢您的意见。在

编辑2: 现在我得到了以日期作为文件名的每个文件的输出。文件现在显示为:

20130813.txt
20130819.txt
20130825.txt

每个文件现在看起来像:

15
17
18
21
14
18
14
13
17
11
11
18
15
15
12
17
9
10
12
17
14
17
13

每一个文件都有200多行。理想的情况是,到现在为止,每一个事件发生很多次,并以最小的数字排序,将是最好的期望结果。在

我尝试过类似的方法:

import sys
from collections import Counter

p = '.txt'
d = []
with open(p, 'r') as f:
    for x in f:
        x = int(x)
        d.append(x)
    d.sort()
    o = Counter(d)
    print o

这有道理吗?在

编辑3:

我有下面的脚本,它对我来说是唯一的,但我仍然无法按唯一计数排序。在

import os
from collections import Counter

p = './newR'
fd = os.listdir(p)

for f in fd:
    pathN = os.path.join(p, f)
    with open(pathN, 'r') as infile:
        fileN = os.path.basename(pathN)
        stripFN = os.path.splitext(fileN)[0]
        fileDate = stripFN.split('_')[0]
        counts = Counter(l.strip() for l in infile)
        for line, count in counts.most_common():
            print line, count

结果如下:

14 291
15 254
12 232
13 226
17 212
16 145
18 127
11 102
10 87
19 64
21 33
20 24
22 15
9 15
23 9
30 6
60 3
55 3
25 3

输出应如下所示:

9 15
10 87
11 102
12 232
13 226
14 291
etc

最有效的方法是什么?在


Tags: 文件pathinimporttxtforosnp
2条回答

下面的代码实现了我最初的问题。在

import os, os.path, subprocess
from sys import stdout

p = './new/results/v4/TRACE_v4_results_ASN_mh60'
fd = os.listdir(p)

def writeFile(fd, flines):
    fo = './new/newR/'+fd+'_v4.txt'
    with open(fo, 'a') as f:    
        r = '%s\n' % (flines)
        f.write(r)

for pfiles in dirs:
pathN = os.path.join(path, pfiles)
files = open(pathN, 'r')
fileN = os.path.basename(pathN)
stripFN = os.path.splitext(fileN)[0]
fileDate = stripFN.split('_')[0]
fdate = fileDate[0:8]
numlines = len(files.readlines()[1:])
writeFile(fdate, numlines)
files.close()

结果如下:

^{pr2}$

如果我没有遵守规则,我真诚地道歉。在

Dictionaries对这样的任务非常适应。如果要递归处理不同目录深度的输入文件,则必须修改下面的示例。还要记住,您可以将Python字符串视为列表,这允许您splice它们(这可以减少混乱的regex)。在

D = {}
fnames = os.listdir("txt/")
for fname in fnames:
    print(fname)
    date = fname[0:8] # this extracts the first 8 characters, aka: date
    if date not in D:
        D[date] = []
    file = open("txt/" + fname, 'r')
    numlines = len(file.readlines())
    file.close()
    D[date].append(fname + " has " + str(numlines) + " lines")

for k in D:
    datelist = D[k]
    f = open('output/' + k + '.txt', 'w')
    for m in datelist:
        f.write(m + '\n')
    f.close()

相关问题 更多 >