Python-从一个文本文件复制到另一个文本

时间:2019-07-02 21:40:22

标签: python io copy benchmarking text-extraction

我正在编写Python脚本,并想将Flexible I / O(FIO)数据文本文件的特定行复制到我的输出文本文件中。

我尝试使用this tutorial从数据文本文件中复制数据,但是很遗憾,我没有得到理想的结果。执行代码并将其连接到output.txt之后,output.txt不会返回任何内容,而不会返回data.txt中的cpu或lat(usec)文本行。

这是我获取data.txt的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import subprocess
import os

cmd = subprocess.Popen(["sudo", "fio", "--filename=/dev/sdg", "--name=test1", "--ioengine=libaio",
    "--bs=4k", "--rw=read", "--iodepth=32", "--runtime=10"],
    stdout = subprocess.PIPE, stderr = subprocess.PIPE, preexec_fn = os.setpgrp)

f = open("logfile.txt", "w+")
g = open("logs.txt", "w+")

f.write(cmd.stdout.read().decode())

with g as f:
    for line in f:
        # finds the leading text in the text file that tarts with "", and also removes all
        # leading and trailing spaces from a strin.
        if line.strip().startswith("lat (usec)") or line.strip().startswith("cpu"):
            # adds items to end of list
            g.write(line + "\n")

这是我的数据文本文件(data.txt):

test1: (g=0): rw=read, bs=4K-4K/4K-4K/4K-4K, ioengine=libaio, iodepth=32
fio-2.16
Starting 1 process

test1: (groupid=0, jobs=1): err= 0: pid=9401: Tue Jul  2 14:28:30 2019
  read : io=4066.2MB, bw=416418KB/s, iops=104104, runt= 10001msec
    slat (usec): min=1, max=2650, avg= 7.75, stdev=41.93
    clat (usec): min=19, max=2790, avg=298.85, stdev=180.63
     lat (usec): min=101, max=2792, avg=306.59, stdev=181.00
    clat percentiles (usec):
     |  1.00th=[  105],  5.00th=[  112], 10.00th=[  118], 20.00th=[  120],
     | 30.00th=[  129], 40.00th=[  137], 50.00th=[  153], 60.00th=[  454],
     | 70.00th=[  474], 80.00th=[  490], 90.00th=[  502], 95.00th=[  516],
     | 99.00th=[  548], 99.50th=[  580], 99.90th=[  756], 99.95th=[  932],
     | 99.99th=[ 1592]
    lat (usec) : 20=0.01%, 100=0.01%, 250=51.56%, 500=36.57%, 750=11.76%
    lat (usec) : 1000=0.06%
    lat (msec) : 2=0.04%, 4=0.01%
  cpu          : usr=18.48%, sys=38.28%, ctx=16300, majf=0, minf=40
  IO depths    : 1=0.1%, 2=0.1%, 4=0.1%, 8=0.1%, 16=0.1%, 32=100.0%, >=64=0.0%
     submit    : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
     complete  : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.1%, 64=0.0%, >=64=0.0%
     issued    : total=r=1041149/w=0/d=0, short=r=0/w=0/d=0, drop=r=0/w=0/d=0
     latency   : target=0, window=0, percentile=100.00%, depth=32

Run status group 0 (all jobs):
   READ: io=4066.2MB, aggrb=416417KB/s, minb=416417KB/s, maxb=416417KB/s, mint=10001msec, maxt=10001msec

Disk stats (read/write):
  sdg: ios=16094/0, merge=0/0, ticks=14376/0, in_queue=14368, util=99.01%

现在我希望从数据文本文件到输出文本文件的第8、15和18行,但是什么也没有返回。这就是输出文本文件的样子(output.txt):

     lat (usec): min=101, max=2792, avg=306.59, stdev=181.00
    lat (usec) : 250=5.09%, 500=67.38%, 750=23.29%, 1000=3.87%
  cpu          : usr=18.48%, sys=38.28%, ctx=16300, majf=0, minf=40

是否有使用上述教程将数据文本文件中的数据提取到结果输出文件中的类似方法?

谢谢!

2 个答案:

答案 0 :(得分:1)

像这样吗?

with open("/tmp/log.txt") as f:
        to_keep = []
        for line in f:
                if line.strip().startswith("lat (usec)") or line.strip().startswith("cpu"):
                        to_keep.append(line)
        print(to_keep)

btw:并不是真的最干净的方法,但是我想最简单的方法(在python中)

更简单的方法是使用简单的linux命令,例如:

   cat log.txt | grep "lat (usec)"

答案 1 :(得分:0)

我发现了问题,谢谢@AlessioM和@pkfm的帮助!

这是我的答案:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import subprocess
import os

def fioCall():
    cmd = subprocess.Popen(["sudo", "fio", "--filename=/dev/sdg", "--name=test1", "--ioengine=libaio",
    "--bs=4k", "--rw=read", "--iodepth=32", "--runtime=10"],
    stdout = subprocess.PIPE, stderr = subprocess.PIPE, preexec_fn = os.setpgrp)

    f = open("data.txt", "w+")
    f.write(cmd.stdout.read().decode())
    f.close()

def copyFiles():
    with open("data.txt") as f:
        with open("output.txt", "w") as f1:
            for line in f:
                if line.strip().startswith("lat (usec)") or line.strip().startswith("cpu"):
                    f1.write(line)

def main():
    fioCall()
    copyFiles()

if __name__ == "__main__":
    main()

这是我获得的输出(output.txt):

     lat (usec): min=161, max=10624, avg=458.78, stdev=148.10
    lat (usec) : 250=5.09%, 500=67.38%, 750=23.29%, 1000=3.87%
  cpu          : usr=17.24%, sys=63.28%, ctx=81571, majf=0, minf=40