2015-04-02 27 views
1

這個腳本的目的是從目錄的每個文件中提取md5校驗和作爲源代碼,然後(我正在處理它)在目標上執行腳本驗證它已正確複製。python從iteration和子進程保存輸出以進行校驗

#!/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 

此外,我不知道如何解決每次迭代之間的空間。我也在尋找。

這樣做後,我會比較每個項目,以驗證一旦複製到目的地的完整性。

回答

0

啊,有人問替代品,有當然:)

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()) 

我一直在使用這樣的事情是前面。

我喜歡的是使用日誌記錄模塊,因爲它使事物具有可伸縮性,我不必打開文件或繼續打開它。記錄器具有高度的可配置性,但只需要在這裏生成需要的東西,簡單的設置就是單線程。

這裏我沒有做任何控制檯解析,因爲我使用pythons hashlib來生成文件md5。現在可以說,這樣做可能會減慢速度,但至少對於我通常遇到的文件大小而言,目前爲止我沒有任何問題。

會對測試大文件很有意思,否則記錄機制也可以用在你的案例中。我當時只喜歡hashlib,因爲我不喜歡解析控制檯輸出。

+0

我計劃通過電影文件計算非常大的文件:每幀1.5G有時(.ARI。R3D文件)所以需要確保trasnfers是okey。讓我實現你的代碼看起來非常方便。 – x1c70r 2015-04-02 17:21:32

+0

我擔心這樣的大文件,你可能運行速度更快,從控制檯解析md5,但你仍然可以組合方法。我認爲最大的文件是700 MB左右,但我已經測試了更小的文件 – 2015-04-02 17:42:49

+0

btw。如果你想在服務器上運行相同的作業來比較文件,這是你在「常規」基礎上做的,那麼你應該檢查fabric:http://www.fabfile.org – 2015-04-02 17:44:43

0

你繼續打開w並覆蓋,打開a追加。

,最好的辦法是簡單地將stdout到一個文件對象,是這樣的:

def checa_sum(x): 
    with open(archivo,'a') as outfile: 
     check_call(["md5",x], stdout=outfile) 
使用 check_call將提高,你應該相應地處理一個非零退出狀態 CalledProcessError

捕獲異常:

try: 
    check_call(["md5sum", x], stdout=outfile) 
    except CalledProcessError as e: 
    print("Exception for {}".format(e.cmd)) 

用生成器表達式來獲取文件,如果你想忽略副本使用not f.endswith("~")

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) 
+0

謝謝。基本上這樣做。謝謝!任何其他改進措施都非常受歡迎。 – x1c70r 2015-04-02 15:39:26

+0

@ x1c70r,除了可能使用一個生成器表達式而不是你的os.listdir調用一切看起來很好 – 2015-04-02 15:42:33