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.

(defun xah-python-2to3-current-file ()
  "Convert current buffer from python 2 to python 3.
This command calls python3's script 「2to3」.
URL `http://ergoemacs.org/emacs/elisp_python_2to3.html'
Version 2016-02-16"
  (interactive)
  (let* (
         (fName (buffer-file-name))
         (fSuffix (file-name-extension fName)))
    (when (buffer-modified-p)
      (save-buffer))
    (if (or (string-equal fSuffix "py") (string-equal fSuffix "py3"))
        (progn
          (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: Write Emacs Commands Using Python, Ruby, …

Elisp Commands Do thing-at-point

  1. Count Words
  2. Run Current File
  3. Delete Current File
  4. Copy File Path
  5. Convert Line Ending
  6. Make Backup
  7. Copy Column of Text to Clipboard
  8. Convert Image File
  9. Dired Rename Space to Hyphen
  10. Python 2to3 Wrapper
  11. Move Code to Files
  12. Update Web Feed
  13. Updating Page Tags
  14. Single Key Copy/Paste Register

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc