[Tutor] Easier way to use 2to3?

Cameron Simpson cs at cskk.id.au
Fri Aug 23 21:10:27 EDT 2019


On 23Aug2019 20:49, Ken Green <beachkidken at gmail.com> wrote:
>It took me a while to figure out how to do a 2to3
>as shown in several examples I seen, [...]

Documentation beats examples.

[...]
>When I ran 2to3 sample.py in a Python3 directory,
>I get the following:
>
>ken at kengreen:~/Python3$ 2to3 sample.py
[... noise, diff, original file unchanged ...]
>Huh, what? The original program was not changed! I was
>expecting a changed Python2 program to be compatible with
>Python3. I ended up making the needed changes line-by-line
>to my original program. What gives?

The default mode does no damage to files. But if we ask 2to3 for help:

    [~]fleet*> 2to3 --help
    Usage: 2to3 [options] file|dir ...

    Options:
      -h, --help            show this help message and exit
      -d, --doctests_only   Fix up doctests only
      -f FIX, --fix=FIX     Each FIX specifies a transformation; default: all
      -j PROCESSES, --processes=PROCESSES
                            Run 2to3 concurrently
      -x NOFIX, --nofix=NOFIX
                            Prevent a transformation from being run
      -l, --list-fixes      List available transformations
      -p, --print-function  Modify the grammar so that print() is a function
      -v, --verbose         More verbose logging
      --no-diffs            Don't show diffs of the refactoring
      -w, --write           Write back modified files
      -n, --nobackups       Don't write backups for modified files
      -o OUTPUT_DIR, --output-dir=OUTPUT_DIR
                            Put output files in this directory instead of
                            overwriting the input files.  Requires -n.
      -W, --write-unchanged-files
                            Also write files even if no changes were required
                            (useful with --output-dir); implies -w.
      --add-suffix=ADD_SUFFIX
                            Append this string to all output filenames.  Requires
                            -n if non-empty.  ex: --add-suffix='3' will generate
                            .py3 files.

So try the -w option.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list