Unable to loop the files to perform diff in python
<p>I am new to python. I am writing a python script to find the diff between 2 html file1: beta.vidup.me-log-2016-09-21-17:43:28.html and file2: beta.vidup.me-log-2016-09-21-17:47:48.html.</p>

<p>To give an idea about my file organization: I have 2 directories 2016-09-21 and 2016-09-22. file1: beta.vidup.me-log-2016-09-21-17:43:28.html is present in dir1 and file2: beta.vidup.me-log-2016-09-21-17:47:48.html is present in dir2. </p>

<p>Below is my snippet:</p>

<pre><code>dir1 = raw_input("Enter date of Archive folder to compare with in format yyyy-mm-dd----&gt;\n")
dir2 = raw_input("Enter date of folder to compare in format yyyy-mm-dd-----&gt;\n")

now = 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 for 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()
</code></pre>

<p>I wish to perform diff using the subprocess module only.</p>

<p>I want my code to take fname1 from dir1 and fname2 from dir2 and perform the diff and output it to a folder and Loop back to pick the next file in dir1 for fname1 and next file from dir2 for fname2 and perform the diff again.</p>

<p>Thanks for your time and advice in advance. </p>

<p>Below are the functions you need with examples. Merge them using the logic with <code>for</code> loop. </p>

<p>You can use <a href="https://docs.python.org/2/library/subprocess.html#subprocess.check_output" rel="nofollow"><code>subprocess.check_output()</code></a> to get output from the command. Try:</p>

<pre><code>cmd = ["diff", fname1, +fname2]
output = subprocess.check_output(cmd)
print output
</code></pre>

<p>if you want to write this to file:</p>

<pre><code>with open('/paht/to/file', 'w+') as f:
    f.write(output)
</code></pre>

<p>In order to get list of files in directory, use <code>listdir()</code>, <code>isfile</code> and <code>join</code> functions of <a href="https://docs.python.org/2/library/os.html" rel="nofollow"><code>os</code></a> module. For example:</p>

<pre><code>from os import listdir
from os.path import isfile, join
only_files = [f for f in listdir('path') if isfile(join('path', f))]
# only_files will contain list of all files in 'path' path
</code></pre>

<hr>

<p>As you mentioned, you don't have much idea on loop. To give you the basic idea on how your loop should work. Below is the example (instead of copying, try to understand each line. it'll be helpful for you in future):</p>

<pre><code>for f1, f2 in zip(file_list_1, file_list_2): # takes first, second, etc files corresponding to each list 
    output = subprocess.check_output(['diff', f1, f2]) # generate diff of both file
    with open('diff-{}-{}'.format(f1, f2), 'w+') as f:
        f.write(output)  # write the diff to third file
</code></pre>

<p>Modify the above logic as per your requirement.        </p>

python subprocess diff