python节省输出从迭代和子进程校验和 [英] python saving output from a for iteration and subprocess for checksum

查看:155
本文介绍了python节省输出从迭代和子进程校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个脚本的目的是从目录的每个文件中提取md5校验和作为源代码,然后(我正在处理它)执行目标上的脚本,以便验证它已经正确复制。


$ b $

 #!/ usr / bin / env python 

import sys $ b $ from sys import *
导入子流程


脚本,路径= argv

destination =./new_directorio/
archivo =cksum.txt


def checa_sum(x):
ck =md5%s%x
p = subprocess.Popen(ck,stdout = subprocess.PIPE,shell = True)
(output,err)= p.communicate()
$ b out = open(archivo,'w')
out.write(%s%输出))
out.close()

files = [f for os.listdir(path)if os.path.isfile(f)]
for i in文件:
如果不是〜在$:
checa_sum(i)

给我的是一个名为cksum.txt
的文件,但文件内只有一个结果。

  bash-3.2 $ more cksum.txt 
MD5(victor)= 4703ee63236a6975abab75664759dc29
bash-3.2 $

另一个尝试,而不是打开,写,闭 e结构使用以下内容:

$ p $ code $ def checa_sum(x):
ck =md5%s% x
p = subprocess.Popen(ck,stdout = subprocess.PIPE,shell = True)
(output,err)= p.communicate()

with open(archivo, 'w')as outfile:
outfile.write(输出)

我得到一个结果,当我期望在文件中的以下结果?

  MD5(pysysinfo.py)= 61a532c898e6f461ef029cee9d1b63dd 

MD5(pysysinfo_func.py)= ac7a1c1c43b2c5e20ceced5ffdecee86

MD5(pysysinfo_new.py)= 38b06bac21af3d08662d00fd30f6c329

MD5(test)= b2b0c958ece30c119bd99837720ffde1

MD5(test_2.py)= 694fb14d86c573fabda678b9d770e51a

MD5(uno.txt)= 466c9f9d6a879873688b000f7cbe758d

MD5(胜者)= 4703ee63236a6975abab75664759dc29
/ pre>

另外,我不知道如何填写ckle每次迭代之间的空间。我也在找



完成此操作后,我将比较每个项目以验证一旦复制到目标的完整性。

  

导入日志
import hashlib
import os $ b $ outfile =hash.log
indir =/ Users / daniel / Sites / work
logging.basicConfig(filename = outfile,filemode =w,format ='%(message)s',level = logging.DEBUG)
用于文件名(os.listdir(indir)中的文件)if os.path.isfile文件)而不是file.endswith(〜)):
打开(文件名)作为checkfile:
logging.info(hashlib.md5(checkfile.read())。hexdigest())

以前我一直在使用这样的东西。



我喜欢使用日志记录模块,因为它使事情变得可伸缩,我不必打开文件或继续打开它。记录器是高度可配置的,但只是在这里生成需要的东西,简单的设置是一个班轮。

这里我没有做任何控制台解析,因为我使用pythons hashlib 生成文件md5。现在可以说,这样做可能会放慢速度,但至少对于我通常遇到的文件大小来说,目前为止我没有任何问题。

会有趣的测试在较大的文件,否则记录机制也可以在你的情况下使用。我只喜欢hashlib,因为我不喜欢解析控制台输出。

The purpose of this script is to pull md5 checksum from each file of a directory as source and then (I'm working on that also) execute the script on the destination so validate it has copied correctly.

#!/usr/bin/env python

import os
from sys import *
import subprocess


script, path = argv

destination = "./new_directorio/"
archivo = "cksum.txt"


def checa_sum(x):
        ck = "md5 %s" % x
        p = subprocess.Popen(ck, stdout=subprocess.PIPE, shell=True)
        (output, err) = p.communicate()

        out = open(archivo,'w')
        out.write("%s" % (output))
        out.close()

files = [f for f in os.listdir(path) if os.path.isfile(f)]
for i in files:
        if not "~" in i:
                checa_sum(i)

What gives me is a file called: "cksum.txt" but only one result inside the file.

bash-3.2$ more cksum.txt
MD5 (victor) = 4703ee63236a6975abab75664759dc29
bash-3.2$ 

An other try, instead of "open", "write", "close" structure is using the following:

def checa_sum(x):
            ck = "md5 %s" % x
            p = subprocess.Popen(ck, stdout=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()

             with open(archivo,'w') as outfile:
                   outfile.write(output)

Why is only dropping me one result when I expect the following result in the file?:

MD5 (pysysinfo.py) = 61a532c898e6f461ef029cee9d1b63dd

MD5 (pysysinfo_func.py) = ac7a1c1c43b2c5e20ceced5ffdecee86

MD5 (pysysinfo_new.py) = 38b06bac21af3d08662d00fd30f6c329

MD5 (test) = b2b0c958ece30c119bd99837720ffde1

MD5 (test_2.py) = 694fb14d86c573fabda678b9d770e51a

MD5 (uno.txt) = 466c9f9d6a879873688b000f7cbe758d

MD5 (victor) = 4703ee63236a6975abab75664759dc29

Moreover, I don't know how to tackle the space between each iteration. I'm looking for that too.

After having this, I'm going to compare each item to verify the integrity once is copied to the destination.

解决方案

ah, someone asked for alternatives, there are of course :)

import logging
import hashlib
import os
outfile = "hash.log"
indir = "/Users/daniel/Sites/work"
logging.basicConfig(filename=outfile, filemode="w", format='%(message)s', level=logging.DEBUG)
for filename in (file for file in os.listdir(indir) if os.path.isfile(file) and not file.endswith("~")):
    with open(filename) as checkfile:
        logging.info(hashlib.md5(checkfile.read()).hexdigest())

i've been using something like this before.

what i like is using the logging module, because it makes things scalable, i don't have to keep a file open, or keep on opening it. the logger is highly configurable, but for just generating something like needed here, the simple setup is a one liner.

here i am not doing any console parsing, because i am using pythons hashlib to generate the file md5. now one could say, doing this could be slowing things down, but at least for the file sizes i usually encounter i had no problems so far.

would be interesting to test on larger files, otherwise the logging mechanism could also be used in your case. i only preferred hashlib back then, because i did not fancy parsing console output.

这篇关于python节省输出从迭代和子进程校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆