Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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保存迭代的输出和校验和的子进程的输出_Python_For Loop_Subprocess_Md5 - Fatal编程技术网

python保存迭代的输出和校验和的子进程的输出

python保存迭代的输出和校验和的子进程的输出,python,for-loop,subprocess,md5,Python,For Loop,Subprocess,Md5,这个脚本的目的是从目录的每个文件中提取md5校验和作为源,然后(我也在处理这个问题)在目标上执行脚本,以便验证它是否正确复制 #!/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 %

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

#!/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)
给我的是一个名为“cksum.txt”的文件 但文件中只有一个结果

bash-3.2$ more cksum.txt
MD5 (victor) = 4703ee63236a6975abab75664759dc29
bash-3.2$ 
另一种尝试(而不是“打开”、“写入”、“关闭”结构)使用以下内容:

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)
当我希望文件中出现以下结果时,为什么只删除一个结果

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
此外,我不知道如何处理每次迭代之间的空间。我也在找


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

您继续使用
w
打开并覆盖,使用
a
打开以附加

最好的方法是简单地将stdout重定向到文件对象,例如:

def checa_sum(x):
    with open(archivo,'a') as outfile:
        check_call(["md5",x], stdout=outfile)
使用
check\u call
将引发非零退出状态的
CalledProcessError
,您应相应地处理该状态

要捕获异常,请执行以下操作:

  try:
     check_call(["md5sum", x], stdout=outfile)
  except CalledProcessError as e:
     print("Exception for {}".format(e.cmd))
使用生成器表达式获取文件,如果要忽略副本,请使用
而不是f.endswith(“~”)


啊,有人要求另类,当然有:)

我以前用过类似的东西

我喜欢的是使用日志记录模块,因为它使事情具有可伸缩性,我不必保持文件打开或继续打开它。记录器是高度可配置的,但对于这里所需的内容,简单的设置是一行程序

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


在较大的文件上测试会很有趣,否则日志机制也可以在您的案例中使用。当时我只喜欢hashlib,因为我不喜欢解析控制台输出

哦,谢谢。基本上就是这样。谢谢任何其他改进都是非常受欢迎的。@x1c70r,除了可能使用生成器表达式而不是os.listdir调用的列表之外,一切看起来都很好,我计划对通常非常大的胶片文件进行计算:有时每帧1.5G(.ARI.R3D文件),所以需要确保传输正常。让我来实现您的代码,因为它看起来非常方便。我担心对于如此大的文件,从控制台解析md5可能会运行得更快,但您仍然可以组合方法。我想我拥有的最大文件大约是700MB,但我现在已经在较小的文件上进行了测试。如果您希望在服务器上运行相同的作业来比较文件,并且这是您“定期”执行的操作,那么您应该检查fabric:这些文件将在本地进行比较。但一旦他们进入云端,作为参考是很好的。非常感谢。让我带着我的代码回去给你们看,看看有没有什么改进。
files = (f for f in os.listdir("/home/padraic") if os.path.isfile(f) and not f.endswith("~"))
for i in files:
    checa_sum(i)
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())