A Python utility to simplify the writing of shell-like scripts.
Project description
Introduction
Documentation | Changelog | Repository
shell-lib is designed to simplify the writing of shell-like scripts:
- Python syntax: Write scripts in readable Python, freeing from complex shell command syntax.
- Reliable error handling: Use Python's exception to manage command failure. If a command fails, by default, it raises a
subprocess.CalledProcessErrorexception. For commands that may fail, user can also only check the exit-code. - Cross-platform compatibility: Write a single script that works across Linux, macOS, and Windows platforms.
- Rich ecosystem integration: Easily integrate with both the CLI tool and Python library ecosystems.
- Lightweight and portable: Only use Python standard library.
- Well tested: Consistent and reliable behavior on different platforms and Python versions.
- Friendly: Readers can understand the code without consulting the documentation. If writers use modern IDEs, then almost no need to consult the documentation.
It provides some functions that make writing shell-like scripts easy:
- Executing external command: Conveniently invoke external commands.
- File and directory operations: Provide a consistent and intuitive file system operations API, that clearly distinguish between file and directory operations.
- User interactions: Do common interactions with one line of code.
- Obtaining system information: Get common system information for shell-like scripts.
This module was co-created with Google Gemini.
Usage
#!/usr/bin/python3
from shell_lib import sh
PROJECT_PATH = "my_project"
FILE = "hello.txt"
sh.create_dir(PROJECT_PATH)
# sh.cd() context manager restores the previous working directory when
# exiting the code block, even if an exception raised within the code block.
with sh.cd(PROJECT_PATH):
sh(f"echo 'Hello, World!' > {FILE}")
print(f"File size: {sh.get_file_size(FILE)} bytes")
sh.remove_dir(PROJECT_PATH)
There is a demo script at the documentation page.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shell_lib-2.0.2.tar.gz.
File metadata
- Download URL: shell_lib-2.0.2.tar.gz
- Upload date:
- Size: 29.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6077391d8f19cbb5b812a9dc8db61beea440451e73c80d8ab979c66d273a8f0c
|
|
| MD5 |
19fcff92b5bb1145d3694cbd2b914313
|
|
| BLAKE2b-256 |
cee88af4eb1c62496c9c49de5ab6a9923f4dcf0579344cdf63320dcfcdd8362e
|
File details
Details for the file shell_lib-2.0.2-py3-none-any.whl.
File metadata
- Download URL: shell_lib-2.0.2-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5e696d516896c357fa0a400826b2b7e4c65581bc454b5855799b00639e0c3b4
|
|
| MD5 |
9b6634bfd29f9017d136438de0d094db
|
|
| BLAKE2b-256 |
97fc9e49d90e3a23ddfca6a280dd188be375d67cf7566eb3c6455134ee555499
|