Note
This is in very alpha state.
And probably as far as it goes as it turns out it has a lot in common with PEP-355.
Simple and brief path traversal and filesystem access library. This library is a bit different that other path manipulation libraries - the principles of this library:
- Path are subclasses of strings. You can use them anyhere you would use a string.
- All the function that works with paths from
os
/os.path
/shutil
should be available. - Transparent support for files in .zip files (with limited functionality).
- Readonly functions are available as properties property. If the function would have side-effects (
chdir
,chroot
etc) then it will be a method. - Original names of the functions are kept. [1]
- Shorthands and brief aliases. Example:
os.path.getsize
becomes a property namedsize
os.path.getatime
becomes a property namedatime
os.path.getctime
becomes a property namedctime
os.path.getmtime
becomes a property namedmtime
os.path.basename
becomes a property namedname
os.path.dirname
becomes a property nameddir
os.listdir
becomes a property namedlist
- Calling a Path object calls
open()
on the path. Takes any argumentopen
would take (except the filename ofcourse).
[1] | However there are few exceptions:
|
Basically it is designed for extreme brevity. It shares Unipath's str-subclassing approach and and it has seamless zip support (like Twisted's ZipPath).
Getting started:
>>> import pth >>> pth # the module is a function! <function pth at ...> >>> p = pth("a.txt") >>> p <Path 'a.txt'> >>> p <Path 'a.txt'>
os /os.path /shutil |
pth.Path |
pth.ZipPath support? |
Notes |
---|---|---|---|
os.access(p, mode) |
p.access(mode) |
✖ | Test access with given mode. |
os.access(p, os.R_OK) |
or
|
✖ | Test read access. |
os.access(p, os.W_OK) |
or
|
✖ | Test write access. |
os.access(p, os.R_OK|os.X_OK) |
or
|
✖ | Test execute access. |
os.chdir(p) |
or
or
|
✖ | Change current directory. |
os.chflags(p, flags) |
p.chflags(flags) |
✖ | Change path flags. |
os.chmod(p, 0644) |
p.chmod(0644) |
✖ | Change mode (permission bits). |
os.chown(p, uid, gid) |
p.chown(uid, gid) |
✖ | Change ownership. |
os.chroot(p) |
p.chroot() |
✖ | Change the root directory of the current process. |
os.getcwd() |
or
|
― | Get current directory. |
os.fsencode(p) |
or
|
✖ | Encode path to filesystem encoding. |
os.fsdecode(p) |
pth(os.fsdecode(p)) |
✖ | Decode path in filesystem encoding. |
os.get_exec_path(env=None) |
✖ | ✖ | Returns the list of paths from $PATH. |
os.lchflags(p, flags) |
or
|
✖ | Change path flags. |
os.lchmod(p, 0644) |
p.lchmod(0644) , p.chmod(0644, follow_symlinks=False) |
✖ | Change mode (permission bits) without following symlinks. |
os.lchown(p, uid, gid) |
p.lchown(uid, gid) , p.chown(uid, gid, follow_symlinks=False) |
✖ | Change ownership without following symlinks. |
os.link(src, dst) |
p.link(dst) |
✖ | Make hard link. |
os.link(src, dst, follow_symlinks=False) (Python>=3.3) |
p.link(dst, follow_symlinks=False) (Python>=3.3 only) |
✖ | Make hard link. |
os.listdir(d) |
p.list |
✔ | List directory; return base filenames. |
os.lstat(p) |
p.lstat() |
✖ | Like stat but don't follow symbolic link. |
os.mkdir(d, 0777) |
d.mkdir(0777) |
✖ | Create directory. |
os.makedirs(d, 0777) |
d.makedirs(0777) |
✖ | Create a directory and necessary parent directories. |
os.mkfifo(path, mode=0o666, dir_fd=None) |
d.mkfifo(mode=0o666, dir_fd=None) |
✖ | Create a FIFO (a named pipe). |
os.open(path, ...) |
✖ | ✖ | Low-level file open (returns fd). |
os.pathconf(p, name) |
p.pathconf(name) |
✖ | Return Posix path attribute. |
os.path.abspath(p) |
p.abs , p.abspath |
✔ | Returns an absolute path. |
os.path.basename(p) |
p.name , p.basename |
✔ | The last component. |
os.path.commonprefix(p) |
✖ | ✖ | Common prefix that can generate invalid paths. |
os.path.dirname(p) |
p.dirname , p.dir |
✔ | All except the last component. |
os.path.exists(p) |
p.exists |
✔ | Does the path exist? |
os.path.lexists(p) |
p.lexists |
✖ | Does the symbolic link exist? |
os.path.expanduser(p) |
p.expanduser |
✔ | Expand "~" and "~user" prefix. |
os.path.expandvars(p) |
p.expandvars |
✔ | Expand "$VAR" environment variables. |
os.path.getatime(p) |
p.atime |
✖ | Last access time. |
os.path.getmtime(p) |
p.mtime |
✖ | Last modify time. |
os.path.getctime(p) |
p.ctime |
✔ | Platform-specific "ctime". |
os.path.getsize(p) |
p.size |
✔ | File size. |
os.path.isabs(p) |
p.isabs |
✔ | Is path absolute? |
os.path.isfile(p) |
p.isfile |
✔ | Is a file? |
os.path.isdir(p) |
p.isdir |
✔ | Is a directory? |
os.path.islink(p) |
p.islink |
✔ | Is a symbolic link? |
os.path.ismount(p) |
p.ismount |
✔ | Is a mount point? |
os.path.join(p, "foobar") |
or
or
|
✔ | Join paths. |
os.path.normcase(p) |
p.normcase |
✔ | Normalize case. |
os.path.normpath(p) |
p.normpath |
✔ | Normalize path. |
os.path.normcase(
os.path.normpath(p)) |
p.norm |
✔ | Normalize case and path. |
os.path.relpath(p, q) |
or
|
✔ | Relative path. |
os.path.realpath(p) |
or
|
✔ | Real path without symbolic links. |
os.path.samefile(p, q) |
or
|
✔ | True if both paths point to the same filesystem item. |
os.path.split(p) |
or
or
|
✔ | Split path at basename. |
os.path.splitdrive(p) |
or
|
✔ | |
os.path.splitext(p) |
or
|
✔ | Split at extension. |
os.path.splitunc(p) |
✖ | ✖ | |
os.path.walk(p, func, args) |
✖ | ✖ | It's deprecated in Python 3 anyway |
os.readlink(p) |
p.readlink |
✖ | Return the path a symbolic link points to. |
os.remove(p) |
p.remove() |
? | Delete file. |
os.removedirs(d) |
d.removedirs |
? | Remove empty directory and all its empty ancestors. |
os.rename(src, dst) |
p.rename(dst) |
? | Rename a file or directory atomically (must be on same device). |
os.renames(src, dst) |
p.renames(dst) |
? | Combines os.rename, os.makedirs, and os.removedirs. |
os.rmdir(d) |
d.rmdir() |
? | Delete empty directory. |
os.stat(p) |
p.stat() |
? | Return a "stat" object. |
os.statvfs(p) |
p.statvfs |
? | Return a "statvfs" object. |
os.symlink(src, dst) |
p.symlink(dst) |
? | Create a symbolic link. ("write_link" argument order is opposite from Python's!) |
os.unlink(f) |
f.unlink() |
? | Same as .remove(). |
os.walk(p) |
p.tree |
✔ | Recursively yield files and directories. |
os.utime(p, times) |
p.utime(times) |
? | Set access/modification times. |
shutil.copyfile(src, dst) |
f.copy(dst) |
? | Copy file. Unipath method is more than copyfile but less than copy2. |
shutil.copy(src, dst) |
f.copy(dst) |
? | High-level copy a la Unix "cp". |
A temporary path:
>>> t = pth.TempPath() >>> t <TempPath '/tmp/...'> >>> with t: ... with (t/"booo.txt")('w+') as f: ... _ = f.write("test") ... print([i for i in t.tree]) [<Path '/tmp/.../booo.txt'>] >>> t.exists False