subprocess - Unable to loop the files to perform diff in python -
i new python. writing python script find diff between 2 html file1: beta.vidup.me-log-2016-09-21-17:43:28.html , file2: beta.vidup.me-log-2016-09-21-17:47:48.html.
to give idea file organization: have 2 directories 2016-09-21 , 2016-09-22. file1: beta.vidup.me-log-2016-09-21-17:43:28.html present in dir1 , file2: beta.vidup.me-log-2016-09-21-17:47:48.html present in dir2.
below snippet:
dir1 = raw_input("enter date of archive folder compare in format yyyy-mm-dd---->\n") dir2 = raw_input("enter date of folder compare in format yyyy-mm-dd----->\n") = datetime.now() folder_output = '/home/diff_output/{}'.format(now.strftime('%y-%m-%d')) mkdir(folder_output) fname1 = '/home/output/%s/beta.vidup.me-log-2016-09-21-17:43:28.html'%dir1 fname2 = '/home/output/%s/beta.vidup.me-log-2016-09-21-17:47:48.html'%dir2 # open file reading in text mode (default mode) f1 = open(fname1) f2 = open(fname2) cmd = "diff "+fname1+'\t'+fname2 curl = subprocess.popen(cmd,shell=true,stdout=subprocess.pipe) file_data = curl.stdout.read() print file_data fname1.close() fname2.close()
i wish perform diff using subprocess module only.
i want code take fname1 dir1 , fname2 dir2 , perform diff , output folder , loop pick next file in dir1 fname1 , next file dir2 fname2 , perform diff again.
thanks time , advice in advance.
below functions need examples. merge them using logic for
loop.
you can use subprocess.check_output()
output command. try:
cmd = ["diff", fname1, +fname2] output = subprocess.check_output(cmd) print output
if want write file:
with open('/paht/to/file', 'w+') f: f.write(output)
in order list of files in directory, use listdir()
, isfile
, join
functions of os
module. example:
from os import listdir os.path import isfile, join only_files = [f f in listdir('path') if isfile(join('path', f))] # only_files contain list of files in 'path' path
as mentioned, don't have idea on loop. give basic idea on how loop should work. below example (instead of copying, try understand each line. it'll helpful in future):
for f1, f2 in zip(file_list_1, file_list_2): # takes first, second, etc files corresponding each list output = subprocess.check_output(['diff', f1, f2]) # generate diff of both file open('diff-{}-{}'.format(f1, f2), 'w+') f: f.write(output) # write diff third file
modify above logic per requirement.
Comments
Post a Comment