https://github.com/hygull/try-pathlib
A repository containing basic usage examples of Python's pathlib library with beautiful documentation. Visit and check beautiful documentation at
https://github.com/hygull/try-pathlib
os pathlib python34
Last synced: 2 months ago
JSON representation
A repository containing basic usage examples of Python's pathlib library with beautiful documentation. Visit and check beautiful documentation at
- Host: GitHub
- URL: https://github.com/hygull/try-pathlib
- Owner: hygull
- License: mit
- Created: 2019-02-28T16:54:50.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-04T08:27:50.000Z (about 6 years ago)
- Last Synced: 2025-01-22T17:13:41.991Z (4 months ago)
- Topics: os, pathlib, python34
- Language: Python
- Homepage: https://hygull.github.io/try-pathlib/
- Size: 5.42 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Python's pathlib - usage examples
This documenation presents basic usage examples of Python's **pathlib** library. Before trying examples in this page make sure to check your Python version by reading the below note or [visit here](https://pypi.org/project/pathlib/?fbclid=IwAR0-Jk14-IkTLCNldiHXvv5HAzhE_C71YDMdv9vmfT0QtlILYZku_7gLKSM) to check.

## What it offers, what speciality it has?
**pathlib** offers a set of classes to handle filesystem paths. It offers the following advantages over using string objects:
+ No more cumbersome use of os and os.path functions. Everything can be done easily through operators, attribute accesses, and method calls.
+ Embodies the semantics of different path types. For example, comparing Windows paths ignores casing.
+ Well-defined semantics, eliminating any warts or ambiguities (forward vs. backward slashes, etc.).
| pathlib | inheritance |
| --- | --- |
| ||## Note
+ Python **3.2** or later is recommended, but pathlib is also usable with Python **2.7** and **2.6**.
+ From Python **3.4**, pathlib is now part of the standard library. For Python **3.3** and earlier, `easy_install pathlib` or `pip install pathlib` should do the trick.
» Mean to say
| Python 2.6 - Python3.3 | >= Python 3.4 |
| --- | --- |
| `pip install pathlib` OR | No installation is required just try it |
| `easy_install pathlib` | Now it's the part of Python's standard library |> Mine is Python **3.7.2** and I am trying it on **MAC OS Mojave**.

## Directory structure of working directory
We will be working on [root](./root) directory. This directory has the following structure.
```bash
Rishikeshs-MacBook-Air:try-pathlib hygull$ pwd
/Users/hygull/Projects/Python3/try-pathlib
Rishikeshs-MacBook-Air:try-pathlib hygull$ tree root/```
```bash
root/
├── c
│ └── examples
│ ├── c-main.md
│ └── hello.c
├── cpp
│ ├── docs
│ │ └── notes.md
│ └── hello.cpp
├── doc.md
├── go
│ ├── docs
│ │ ├── links.md
│ │ └── loop.py
│ ├── hello.go
│ └── images
│ ├── go-slices-usage-and-internals_slice-2.png
│ ├── go.jpeg
│ ├── rishikesh.jpeg
│ └── rishikesh.png
├── js
│ ├── hello.js
│ └── try
│ └── examples
│ └── dict-example.py
├── main.md
└── python
├── examples
│ └── go
│ └── slice.go
├── hello.py
└── images
├── python.jpeg
└── rishikesh.webp```
## Getting started
Now, let's move/navigate to **root** directory which is to be assumed as the working directory in this documentation.
```bash
Rishikeshs-MacBook-Air:try-pathlib hygull$ cd root/
Rishikeshs-MacBook-Air:root hygull$ ls
c cpp doc.md go js main.md python
Rishikeshs-MacBook-Air:root hygull$```
```bash
Rishikeshs-MacBook-Air:root hygull$ python3
Python 3.7.2 (default, Jan 13 2019, 12:50:01)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>```
We are done, let's start.

#### » Listing out directories and files of current directory separately
```python
>>> from pathlib import Path
>>>
>>> root = Path(".")
>>> root
PosixPath('.')
>>>
``````python
>>> directories = [dir_content for dir_content in root.iterdir() if dir_content.is_dir()]
>>> directories
[PosixPath('go'), PosixPath('python'), PosixPath('js'), PosixPath('cpp'), PosixPath('c')]
>>>
``````python
>>> files = [dir_content for dir_content in root.iterdir() if dir_content.is_file()]
>>> files
[PosixPath('doc.md'), PosixPath('main.md')]
>>>```
#### » Getting absoute path of `root` directory
```python
>>> root.absolute()
PosixPath('/Users/hygull/Projects/Python3/try-pathlib/root')
>>>```
#### » Getting `home` directory's absolute path
```python
>>> root.home()
PosixPath('/Users/hygull')```
#### » Listing out all python files present in any of the directories availble under current directory
```python
>>> list(root.glob("**/*.py"))
[PosixPath('go/docs/loop.py'), PosixPath('python/hello.py'), PosixPath('js/try/examples/dict-example.py')]
>>>```
#### » Navigating to `root/python/examples/go` and lisiting out its content(s).
+ Brief look using terminal.
```bash
Rishikeshs-MacBook-Air:root hygull$
Rishikeshs-MacBook-Air:root hygull$ ls python/examples/
go
Rishikeshs-MacBook-Air:root hygull$ ls python/examples/go/
slice.go
Rishikeshs-MacBook-Air:root hygull$ cat python/examples/go/slice.go
package mainimport "fmt"
func main() {
a := []int{12, 5, 6, 8}
fmt.Print(a)
}```
+ Prgramatically navigating to `root/python/examples/go` by verifying the navigated location's existence.
```python
>>> root
PosixPath('.')
>>>
>>> python = root / "python"
>>> python
PosixPath('python')
>>>
>>> python.exists()
True
>>>
>>> examples = python / "examples"
>>> examples
PosixPath('python/examples')
>>>
>>> examples.exists()
True
>>>
>>> go = examples / "go"
>>> go
PosixPath('python/examples/go')
>>>
>>> go.exists()
True
>>>```
+ Lisiting out content(s) of **go** directory.
```python
>>> list(go.iterdir())
[PosixPath('python/examples/go/slice.go')]
>>>
```+ Single line to navigate to `go` directory - `go = root / "python" / "examples" / "go"`
```python
>>> examples = root / "examples"
>>> examples.exists()
False
>>>
>>> examples = root / "python" / "examples"
>>> examples.exists()
True
>>>
``````python
>>> go = root / "python" / "examples" / "go"
>>> go.exists()
True
>>>```
#### » Getting URI
```python
>>> python
PosixPath('python')
>>>
``````python
>>> python.as_uri()
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pathlib.py", line 714, in as_uri
raise ValueError("relative path can't be expressed as a file URI")
ValueError: relative path can't be expressed as a file URI
>>>
``````python
>>> python = python.resolve()
>>> python
PosixPath('/Users/hygull/Projects/Python3/try-pathlib/root/python')
>>>
``````python
>>> python.as_uri()
'file:///Users/hygull/Projects/Python3/try-pathlib/root/python'
>>>```
#### » Printing parent directories and checking their existence
```python
>>> go = go.resolve()
>>> go.as_uri()
'file:///Users/hygull/Projects/Python3/try-pathlib/root/python/examples/go'
>>>```
```python
>>> examples = go.parent
>>> examples
PosixPath('/Users/hygull/Projects/Python3/try-pathlib/root/python/examples')
>>>
>>> examples.exists()
True
>>>```
```python
>>> python = examples.parent
>>> python
PosixPath('/Users/hygull/Projects/Python3/try-pathlib/root/python')
>>>
>>> python.exists()
True
>>>
```### » Creating a directory with a file inside it with few lines
> The output of the below code is [created_files](./src/created_files) directory with [message.txt](./src/created_files/message.txt) file inside it.
```python
"""
{
"date_created": "2 march 2019, Sat",
"aim": "Creating files with contents (few lines)",
"created_by": "Rishikesh Agrawani",
"description": "First the code will check if there is a directory
named `created_files` in the current working directory
if it exists, it will create files inside that
it it doesn't exist, create the folder first and then create file
named `message.txt` inside that"}
"""from pathlib import Path
cwd = Path(".")
created_files_dir = cwd / "created_files"if not created_files_dir.exists():
created_files_dir.mkdir()file = created_files_dir / "message.txt"
lines = [
"It is better to learn programming for solving complex problems.",
"Python, Java, Go, C, JavaScript, Ruby, PHP are popular.",
"So finally, we all are in a right place"
]file.write_text('\n'.join(lines))
```# » Creating directories in the current working directory
+ Name of the directories are specified in a text file named [directory_names.txt](../docs/texts/directory_names.txt)
+ Creates the directory names mentioned in the text file
```python
"""
{
"date_created": "4 march 2019, Mon",
"aim": "Creating directories whose names are mentioned in a text file",
"created_by": "Rishikesh Agrawani",
"description": "First program will check if directory named `created_directories` exists or not
If it does exist it creates that and after that it reads a text file
`../docs/texts/directory_names.txt` to read list of directory names and create
those directories inside `created_directories` directory.
Here I have not coded for validations etc. so assume that I have valid names in
the text file (check it)"
}
"""from pathlib import Path
cwd = Path(".").resolve()
created_directories_dir_path = cwd / "created_directories"if not created_directories_dir_path.exists():
created_directories_dir_path.mkdir()
print("Directory successfully created")# Getting path of the file to be read
directory_names_file_path = cwd.parent / "docs" / "texts" / "directory_names.txt"print(directory_names_file_path)
print(directory_names_file_path.exists())
print(directory_names_file_path.is_file())directory_names = directory_names_file_path.read_text().split('\n')
# ['Gayle', 'Sachin', 'Sehwag', 'Garry', 'Dhoni', 'Ricky', 'Adam', 'Ken', 'Dennis', 'Wes']
print(directory_names)for directory_name in directory_names:
directory_path = created_directories_dir_path / directory_name.strip()
directory_path.mkdir()
print("Created", directory_path)# Storing absolute path of all the newly created directories
new_dir_paths = [str(new_dir_path) for new_dir_path in created_directories_dir_path.iterdir() if new_dir_path.is_dir()]
print(new_dir_paths)
# ['/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sehwag', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dennis', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dhoni', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ricky', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Garry', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Gayle', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Wes', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sachin', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ken', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Adam']```
> Ouput
```bash
Rishikeshs-MacBook-Air:src hygull$ python3 create_directories.py
Directory successfully created
/Users/hygull/Projects/Python3/try-pathlib/docs/texts/directory_names.txt
True
True
['Gayle', 'Sachin', 'Sehwag', 'Garry', 'Dhoni', 'Ricky', 'Adam', 'Ken', 'Dennis', 'Wes']
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Gayle
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sachin
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sehwag
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Garry
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dhoni
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ricky
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Adam
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ken
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dennis
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Wes
['/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sehwag', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dennis', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dhoni', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ricky', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Garry', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Gayle', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Wes', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sachin', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ken', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Adam']
```## Help
```python
>>> from pathlib import Path
>>>
>>> root = Path(".")
>>>
>>> dir(root)
['__bytes__', '__class__', '__delattr__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__fspath__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rtruediv__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__truediv__', '_accessor', '_cached_cparts', '_closed', '_cparts', '_drv', '_flavour', '_format_parsed_parts', '_from_parsed_parts', '_from_parts', '_hash', '_init', '_make_child', '_make_child_relpath', '_opener', '_parse_args', '_parts', '_pparts', '_raise_closed', '_raw_open', '_root', '_str', 'absolute', 'anchor', 'as_posix', 'as_uri', 'chmod', 'cwd', 'drive', 'exists', 'expanduser', 'glob', 'group', 'home', 'is_absolute', 'is_block_device', 'is_char_device', 'is_dir', 'is_fifo', 'is_file', 'is_mount', 'is_reserved', 'is_socket', 'is_symlink', 'iterdir', 'joinpath', 'lchmod', 'lstat', 'match', 'mkdir', 'name', 'open', 'owner', 'parent', 'parents', 'parts', 'read_bytes', 'read_text', 'relative_to', 'rename', 'replace', 'resolve', 'rglob', 'rmdir', 'root', 'samefile', 'stat', 'stem', 'suffix', 'suffixes', 'symlink_to', 'touch', 'unlink', 'with_name', 'with_suffix', 'write_bytes', 'write_text']
>>>
>>> help(root)>>> help(root.exists())
>>>
>>> help(root.as_uri)>>>
```
## Screenshots








## References
+ [https://python.readthedocs.io/en/stable/library/pathlib.html](https://python.readthedocs.io/en/stable/library/pathlib.html)
+ [https://pypi.org/project/pathlib/?fbclid=IwAR0-Jk14-IkTLCNldiHXvv5HAzhE_C71YDMdv9vmfT0QtlILYZku_7gLKSM](https://pypi.org/project/pathlib/?fbclid=IwAR0-Jk14-IkTLCNldiHXvv5HAzhE_C71YDMdv9vmfT0QtlILYZku_7gLKSM)
+ [https://realpython.com/python-pathlib/](https://realpython.com/python-pathlib/)