Your friendly Python Path Converter
Python 3.4 introduced Pathlib, a clean object-oriented cross-platform way of handling paths. If the examples in the docs do not convince you yet, please read these blog posts by Trey Hunner: Why you should be using Pathlib and No really, pathlib is great. Don't let your codebases be another one full with unnecessary path logic.
-
Parses your Python code and automatically fix prehistoric file manipulations.
-
Helps you kickstart with refactoring you legacy code paths. Don't expect it to do all the work for you (yet). The best part: even if it makes a mistake, you know already where to look.
-
Profit.
Before:
import os
os.path.isdir(os.path.join(__file__, "../test"))
After:
from pathlib import Path
(Path(__file__) / ".." / "test").is_dir()
You can find more examples in the examples directory.
Installation via pip:
pip install pathlad
Command line interface:
pathlad [dirname]
- Pathlib's equivalences
-
os.makedirs
-
os.path.normpath
-
os.path.realpath
-
open
-
os.path.getsize
-
os.listdir
- Nested calls
-
glob.glob
-
Path.write_*
-
from os import method
Warning: pathlad is under active development, the API might change without notice.
pathlad is built on top of 2to3 (just like black). For now, the options are identical.
Usage: pathlad [options] file|dir ...
Options:
-h, --help show this help message and exit
-j PROCESSES, --processes=PROCESSES
Run 2to3 concurrently
-x NOFIX, --nofix=NOFIX
Prevent a transformation from being run
-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.
-
pathlad
doesn't care about formatting. For the best results, runisort
andblack
afterwards. -
pathlib
is generally slower thanos.path
, especially when creating lots ofPath
objects. In most cases however, such as dataset creation scripts and tests, this doesn't weigh up to the value of readable, maintainable code.
However, being a programmer - I'm too lazy to spend 8 hours mindlessly performing a function, but not too lazy to spend 16 hours automating it.
~ Timothy Crosley
Pull-requests are welcome.
Helpful contributions include:
- Extending API coverage for
os
,glob
,shutils
- More robust fixing w.r.t. spacing, optional arguments etc.
- A method validation only (for example, a
--check-only
flag) - A pre-commit hook
- Typing of some kind
- Basically any feature you see in
black
orisort
etc. - Remove unused
os
,glob
imports (flake8 --select F401
,autoflake --in-place --imports=os,glob,pathlib
)
- http://python3porting.com/2to3.html
- http://python3porting.com/fixers.html
- https://lucumr.pocoo.org/2010/2/11/porting-to-python-3-a-guide/
- https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module
- https://github.com/python/cpython/tree/3.8/Lib/lib2to3/fixes
- https://stackoverflow.com/questions/24508357/how-to-launch-own-2to3-fixer