When you run diff in python and want both returncode and output

I want to get the result of diff with python

There were times when I wanted to run diff in python. Specifically, when you want to take a quick look at the differences between the files you are monitoring.

--Take the result of diff with python --Embed the result in the body of email or chat

Well, you can do everything with a shell, but if you want to do various things before that, python is more convenient.

In such a case, check_output of subprocess is convenient. diff is exit 1 when a diff occurs and check_output throws an exception when not 0. So, the return code and output are included in the exception attributes, so you can pick them up.

sample

subprocess_sample.py


# -*- coding: utf-8 -*-
import subprocess
import shlex
import sys

file1 = sys.argv[1]
file2 = sys.argv[2]

command_line = "diff -u {} {}".format(file1, file2)
command_args = shlex.split(command_line)

rc = 0
try:
    rc = subprocess.check_output(command_args)
except subprocess.CalledProcessError as cpe:
    print "shell returncode is not 0."
    print "returncode: {}".format(cpe.returncode)
    print "output: {}".format(cpe.output)
    rc = cpe.returncode

print rc
% python subprocess_sample.py hoge_old.txt hoge_new.txt
shell returncode is not 0.
returncode: 1
output: --- hoge_old.txt        2015-06-27 14:29:03.000000000 +0900
+++ hoge_new.txt        2015-06-27 14:28:54.000000000 +0900
@@ -1,2 +1,2 @@
-hoge
+hogo
 foo

1

There used to be a commands module, but this is obsolete in Series 3 and is deprecated in Series 2 (2.7 and later).

Recommended Posts

When you run diff in python and want both returncode and output
Export and output files in Python
[Python] When you want to import and use your own package in the upper directory
[Subprocess] When you want to execute another Python program in Python code
[Python] When you want to use all variables in another file
Output python log to both console and file
It is convenient to use stac_info and exc_info when you want to display traceback in log output by logging.
Settings when you want to run python-mecab with travis
python input and output
% And str.format () in Python. Which one do you use?
Use communicate () when receiving output in a Python subprocess
Japanese output in Python
Japanese output when dealing with python in visual studio
When you want to plt.save in a for statement
When you get an error in python scraping (requests)
Recursively search for files and directories in Python and output
I want to do something in Python when I finish
What to do if you run python in IntelliJ and end with an error
[Python3] Code that can be used when you want to resize images in folder units
If you want to use field names with hyphens when updating firestore data in python
Solution when you want to use cv_bridge with python3 (virtualenv)
[Django] A memorandum when you want to communicate asynchronously [Python3]
Sort and output the elements in the list as elements and multiples in Python.
[Selenium] Change log output destination when executing phantomjs in python3
Problems and solutions when asked for MySQL db in Python 3
I want to be able to run Python in VS Code
When you want to hit a UNIX command on Python
Run automatic jobs in python
Run shell commands in python
Attention when os.mkdir in Python
Stack and Queue in Python
Unittest and CI in Python
Read Fortran output in python
Write a script in Shell and Python to notify you in Slack when the process is finished
When you want to replace multiple characters in a string without using regular expressions in python3 series
How to install OpenCV on Cloud9 and run it in Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
Mode line when you open the appropriate Python code in Vim
When you want to print to standard output with print while testing with pytest
I want to run the Python GUI when starting Raspberry Pi
Precautions when passing def to sorted and groupby functions in Python? ??
I want to use both key and value of Python iterator
If you want to assign csv export to a variable in python
[Short text] When returning in both try clause, except clause and finally clause
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Output 2017 Premium Friday list in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Let's run "python -m antigravity" in python
Run shell command / python in R
Behavior when listing in Python heapq
Until you put Python in Docker
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Make standard output non-blocking in Python
Check and move directories in Python
Hashing data in R and Python
Function synthesis and application in Python