Passion : logiciel
mars 22nd, 2011

Compare 2 folders with python and linux

#!/bin/python

import subprocess as sp
import os

def call(arg):
  proc = sp.Popen([arg],shell=True, stdout=sp.PIPE)
  ret = proc.stdout.readlines()
  return ret

dir1 = "recup_dir.1"
dir2 = "recup_dir.2"

rc1 = call("ls "+dir1)
rc2 = call("ls "+dir2)

existList = list()
dontexistList = list()

print dir1 + " against " + dir2
for file in rc1:
  path = "./"+dir2+"/"+file.strip()
  if os.path.isfile(path):
    existList.append(file.strip())
  else:
    dontexistList.append(file.strip())

for exist in existList:
  sumdir1 = call("md5sum ./"+dir1+"/"+exist+"| cut -d\" \" -f1")
  sumdir2 = call("md5sum ./"+dir2+"/"+exist+"| cut -d\" \" -f1")
  if sumdir1 != sumdir2:
    print "different", exist, sumdir1[0].strip(), sumdir2[0].strip()

for dontexist in dontexistList:
  print "dontexist in ./"+dir2+"/"+dontexist

print dir2 + " against " + dir1
for file in rc2:
  path = "./"+dir1+"/"+file.strip()
  if os.path.isfile(path):
    existList.append(file.strip())
  else:
    dontexistList.append(file.strip())

for exist in existList:
  sumdir1 = call("md5sum ./"+dir1+"/"+exist+"| cut -d\" \" -f1")
  sumdir2 = call("md5sum ./"+dir2+"/"+exist+"| cut -d\" \" -f1")
  if sumdir1 != sumdir2:
    print "different", exist, sumdir1[0].strip(), sumdir2[0].strip()

for dontexist in dontexistList:
  print "dontexist in ./"+dir1+"/"+dontexist

0 Comments »

Pas encore de commentaire.

Flux RSS des commentaires de cet article. TrackBack URI

Laisser un commentaire

*
This work is licensed under GPL - 2009 | Powered by Wordpress using the theme aav1