Skip to content

Instantly share code, notes, and snippets.

@bofm
Last active May 5, 2016 09:23
Show Gist options
  • Save bofm/082526ce35f955719e13c12c79d84f53 to your computer and use it in GitHub Desktop.
Save bofm/082526ce35f955719e13c12c79d84f53 to your computer and use it in GitHub Desktop.
Simple function to print diff of two strings
import subprocess
def diff(one, two):
with tempfile.TemporaryDirectory() as tmpdir:
f1, f2 = ('%s/%s' % (tmpdir, n) for n in (1,2))
for f, txt in zip((f1, f2), (one, two)):
with open(f, 'w') as f:
f.write(str(txt))
cmd = ('diff -w %s %s' % (f1, f2)).split()
with subprocess.Popen(cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE) as popen:
out, err = map(bytes.decode, popen.communicate())
print(out)
print(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment