Commits

Antoine Pitrou committed 7db1771

Use the README's examples for the "basic use" section.

Comments (0)

Files changed (1)

 Basic use
 ---------
 
+Importing the module classes::
+
    >>> from pathlib import *
-   >>> p = Path('setup.py')
-   >>> p
-   PosixPath('setup.py')
-   >>> p.is_absolute()
-   False
-   >>> p = p.resolve()
-   >>> p
-   PosixPath('/home/antoine/pathlib/setup.py')
-   >>> p.parent()
-   PosixPath('/home/antoine/pathlib')
-   >>> p.open().readline()
-   '#!/usr/bin/env python3\n'
 
-   >>> import pprint
+Listing Python source files in the current directory::
+
    >>> p = Path('.')
    >>> [x for x in p if x.ext == '.py']
-   [PosixPath('test_pathlib.py'), PosixPath('setup.py'), PosixPath('pathlib.py')]
-   >>> child = p['docs']
-   >>> pprint.pprint(list(child))
-   [PosixPath('docs/conf.py'),
-    PosixPath('docs/_templates'),
-    PosixPath('docs/make.bat'),
-    PosixPath('docs/index.rst'),
-    PosixPath('docs/_build'),
-    PosixPath('docs/_static'),
-    PosixPath('docs/Makefile')]
+   [PosixPath('test_pathlib.py'), PosixPath('setup.py'),
+    PosixPath('pathlib.py')]
+
+Listing subdirectories::
+
+   >>> [x for x in p if x.is_dir()]
+   [PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
+    PosixPath('__pycache__'), PosixPath('build')]
+
+Navigating inside a directory tree::
+
+   >>> p = Path('/etc')
+   >>> q = p['init.d/reboot']
+   >>> q
+   PosixPath('/etc/init.d/reboot')
+   >>> q.resolve()
+   PosixPath('/etc/rc.d/init.d/halt')
+
+Querying path properties::
+
+   >>> q.exists()
+   True
+   >>> q.is_dir()
+   False
+   >>> q.st_mode
+   33261
+
+Opening a file::
+
+   >>> with q.open() as f: f.readline()
+   ...
+   '#!/bin/bash\n'
 
 
 Pure paths
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.