Elisp: Python 2to3 Wrapper 🚀

By Xah Lee. Date: . Last updated: .

Here's a handy emacs lisp wrapper for converting current buffer's python 2 script to python 3.

put this in your Emacs Init File:

(defun xah-python-2to3-current-file ()
  "Convert current buffer from python 2 to python 3.
This command calls python3's script 「2to3」.
URL `http://xahlee.info/emacs/emacs/elisp_python_2to3.html'
Version 2016-02-16"
  (let* (
         (fName (buffer-file-name))
         (fSuffix (file-name-extension fName)))
    (when (buffer-modified-p)
    (if (or (string-equal fSuffix "py") (string-equal fSuffix "py3"))
          (shell-command (format "2to3 -w %s" fName))
          (revert-buffer  "IGNORE-AUTO" "NOCONFIRM" "PRESERVE-MODES"))
      (error "file 「%s」 doesn't end in “.py” or “.py3”." fName))))

A handy command to go with it is Emacs: Run Current File 🚀

See also: Elisp: Call Shell Command