Skip to main content

pathlib_revised is a enhanced version of pathlib

Project description

pathlib_revised

Expand the origin Python pathlib module:

  • work-a-round for Windows MAX_PATH limit, by adding \\?\ path prefix

  • add missing stuff, like: makedirs, utime, scandir etc.

There is also the class, called DirEntryPath that holds more cached information than os.DirEntry

Used in PyHardLinkBackup: https://github.com/jedie/PyHardLinkBackup

requirement: python 3.6 or newer

Please, try, fork and contribute! ;)

Build Status on travis-ci.org

travis-ci.org/jedie/pathlib_revised

Build Status on appveyor.com

ci.appveyor.com/project/jedie/pathlib_revised

Coverage Status on coveralls.io

coveralls.io/r/jedie/pathlib_revised

Requirements Status on requires.io

requires.io/github/jedie/pathlib_revised/requirements/

Windows MAX_PATH

There is a limit in the Windows API: Path can’t be longer than 259 characters (called: “MAX_PATH”). The work-a-round is to add the prefix \\?\ to every absolute path, see:

The Path2() class has the additional property extended_path:

>>> from pathlib_revised import Path2
>>> p=Path2("c:\foo\bar")
>>> p.extended_path
'\\?\c:\foo\bar'

All existing methods of Path2() will internally use extended_path, so that the MAX_PATH limit is not longer a problem.

extended_path exist also under Posix-Systems, but it’s the same as path:

>>> p=Path2("/foo/bar")
>>> p.path
'/foo/bar'
>>> p.extended_path
'/foo/bar'

Additional methods

<pre> >>> Path2(“/”).listdir() [‘sbin’, ‘boot’, ‘tmp’, ‘sys’, ‘var’, ‘dev’, ‘usr’, ‘root’, ‘home’, …, ‘initrd.img’, ‘vmlinuz’] </pre> * shutil.**`copyfile() <https://docs.python.org/3/library/shutil.html#shutil.copyfile>`_**

<pre> >>> Path2(“a_file.txt”).copyfile(Path2(“a_file_copy.txt”)) </pre> * os.path.**`expanduser() <https://docs.python.org/3/library/os.path.html#os.path.expanduser>`_**

<pre> >>> p=Path2(“~”, “sub”, “dir”) >>> p PosixPath2(‘~/sub/dir’) >>> p.expanduser() PosixPath2(‘/home/username/sub/dir’) </pre> * os.**`link() <https://docs.python.org/3/library/os.html#os.link>`_**

<pre> >>> Path2(“source.txt”).link(Path2(“hardlinked.txt”)) </pre> * os.**`makedirs() <https://docs.python.org/3/library/os.html#os.makedirs>`_**

<pre> >>> Path2(“a”, “new”, “path”).makedirs() </pre> * os.**`utime() <https://docs.python.org/3/library/os.html#os.utime>`_**

<pre> >>> mtime = 111111111 # UTC: 1973-07-10 00:11:51 >>> atime = 222222222 # UTC: 1977-01-16 01:23:42

>>> p.Path2("dir/or/file")
>>> p.utime(times=(atime, mtime))
>>> stat = p.stat()
>>> stat.st_atime
222222222
>>> stat.st_mtime
111111111
</pre> * os.**`scandir() <https://docs.python.org/3/library/os.html#os.scandir()>`_**

<pre> >>> p=Path2(“/foo/bar”) >>> for dir_item in p.scandir(): … print(dir_item) … <PosixDirEntry: ‘filename’> <PosixDirEntry: ‘directory’> <PosixDirEntry: ‘…’> </pre> It’s a generator that yields os.**`DirEntry <https://docs.python.org/3/library/os.html#os.DirEntry>`_** instances. scandir is new in Python 3.5, but in Path2() is will fall-back to the external scandir module.

You miss a method? Please, fork, implement, add tests and send a pull request! ;)

DirEntryPath

The DirEntryPath holds more cached information:

instance**.path_instance

Path2() instance

instance**.resolved_path

Path2() instance from .resolve() (If resolve errored: None)

instance**.resolve_error

The error Instance, if .resolve() failed.

instance**.path**

string of the path, same as: str(instance.path_instande)

instance**.is_symlink

bool

instance**.is_file

bool

instance**.is_dir

bool

instance**.stat

bool

Create a instance by feeding a os.DirEntry instance, e.g.:

>>> from pathlib_revised import Path2, DirEntryPath
>>> src_path = Path2("foo/")
>>> for dir_entry in src_path.scandir():
...     dir_entry_path = DirEntryPath(dir_entry)
...     print(dir_entry_path.pformat())
 *** <DirEntryPath foo/file1> :
path...........: 'foo/file1'
path instance..: PosixPath2('foo/file1')
resolved path..: PosixPath2('/home/bar/foo/file1')
resolve error..: None
different path.: True
is symlink.....: False
is file........: False
is dir.........: True
stat.size......: 38
 *** <DirEntryPath foo/BrokenSymlink.ext> :
path...........: 'foo/BrokenSymlink.ext'
path instance..: PosixPath2('foo/BrokenSymlink.ext')
resolved path..: None
resolve error..: FileNotFoundError(2, 'No such file or directory')
different path.: True
is symlink.....: True
is file........: False
is dir.........: False
stat.size......: 15
 *** <DirEntryPath foo/README.creole> :
path...........: 'foo/README.creole'
path instance..: PosixPath2('foo/README.creole')
resolved path..: PosixPath2('/home/bar/foo/README.creole')
resolve error..: None
different path.: True
is symlink.....: False
is file........: True
is dir.........: False
stat.size......: 4802

run tests

e.g.:

~$ git clone https://github.com/jedie/pathlib_revised.git
~$ cd pathlib_revised
~/pathlib_revised$ pipenv install
~/pathlib_revised$ pipenv shell
(pathlib_revised) ~/pathlib_revised$ tox

History

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pathlib_revised-0.2.0rc1.tar.gz (75.9 kB view details)

Uploaded Source

Built Distributions

pathlib_revised-0.2.0rc1-py3.6.egg (12.4 kB view details)

Uploaded Source

pathlib_revised-0.2.0rc1-py2.py3-none-any.whl (24.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pathlib_revised-0.2.0rc1.tar.gz.

File metadata

  • Download URL: pathlib_revised-0.2.0rc1.tar.gz
  • Upload date:
  • Size: 75.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for pathlib_revised-0.2.0rc1.tar.gz
Algorithm Hash digest
SHA256 b8e1f1ea5d6af91adb19a1cf31f31a857f94c6eefda32af577b66f2a6a6fbbfd
MD5 103ad51babd50806af55db107584c37f
BLAKE2b-256 019ad17597b0484c0bcb38759baa039e3ff4a7343a9e45c2a1ca1e96e43c5e86

See more details on using hashes here.

File details

Details for the file pathlib_revised-0.2.0rc1-py3.6.egg.

File metadata

  • Download URL: pathlib_revised-0.2.0rc1-py3.6.egg
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for pathlib_revised-0.2.0rc1-py3.6.egg
Algorithm Hash digest
SHA256 1175436e7610436c70863e02779faa9f9715c900d9c96bbb0c0a4411ae3c2439
MD5 ac4887514535c2433845c648447fbd8c
BLAKE2b-256 760b6cca7228e3005311d2b381f9d3c5b6856ab468bd16f2688f83a15a42b3b7

See more details on using hashes here.

File details

Details for the file pathlib_revised-0.2.0rc1-py2.py3-none-any.whl.

File metadata

  • Download URL: pathlib_revised-0.2.0rc1-py2.py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for pathlib_revised-0.2.0rc1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 95af505713fc90807a6f88e00605cbdc6f8340e9a96f1e2fff8a76c8276e0015
MD5 e7aa64abd5399a90908f990a50dafcf6
BLAKE2b-256 1b674eea35ceb85a772f0a7074ce5d34b7bfb854f5f02122f0910e1ca8ecba28

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page