Pure Python way to apply unified diff to a file?

I have a unified diff file (let me call it a patch). I need to open it, apply to the specified file and save the result back to the file. Same as Unix patch tool. I need a Python solution that I could easily call from my .py script, and so far I cannot find it.

I looked at https://code.google.com/p/google-diff-match-patch/wiki/API and it looks like it cannot do what I need. I also looked at https://github.com/techtonik/python-patch and https://github.com/matiasb/python-unidiff . python-patch seems to emulate Unix patch util, but it's a command line tool, and I don't understand how to call it from my .py script.

+5
source share
1 answer

Using python-patch :

 import patch pset = patch.fromfile(your_unified_diff_file) pset.apply() 

If you want to apply to the stream / output in different ways, you will have to create your own function (see how apply does).

+6
source

All Articles