Typical usage
- Place a file named
.marker-filesomewhere in your file system. - From the directory containing the
.marker-file, or any (nested) subdirectory, you can now use useprojdirto find the path of the directory containing.marker-file:
import projdir
import whatever_package
base_dir = projdir.find(".marker-file") # returns a pathlib.Path object
indata_path = base_dir / "indata"
output_path = base_dir / "result.csv"
whatever_package.create_output_file(indata_path, output_path)
Motivation
- Let's say you are building a Python application (script, command-line tool, whatever) that relies on file system inputs or outputs.
- The expected file inputs or outputs do not have a fixed path compared to the Python script, and you don't want to hard-code an absolute path to the working directory.
- This is similar to Git's
.gitdirectory, which not only contains the repository database but also marks the root directory of the repository.
More examples
Match only files or only directories
base_dir = projdir.find(".marker-file", dir_ok=False) # Match only files
base_dir = projdir.find(".marker-dir", file_ok=False) # Match only dirs
Find a config file
Simple rule: projdir.find() always returns the containing directory. If you want the matching file or directory, this is easily done:
CONFIG_FILE_NAME = ".myappconfig"
base_dir = projdir.find(".myappconfig", dir_ok=False)
config_path = base_dir / CONFIG_FILE_NAME
Use a nested marker file or directory
projdir.find(marker_relpath) always returns a directory base_dir such that base_dir / marker_relpath exists. This holds even if marker_relpath is a nested relative path.
# This will return the outer directory, the one that contains `subdirectory`
base_dir = projdir.find("subdirectory/with/marker-file")
Some technical details
projdir.find()always searches from the current working directory.- If the marker cannot be found in the current working directory, the search continues step by step up the directory tree until the drive root, i.e. "/marker_relpath" on Unix or something like "C:\marker_relpath" on Windows.
- If the marker is not found, the custom
projdir.MarkerNotFoundexception is raised.MarkerNotFoundis a subclass ofFileNotFoundError. projdir.find()always returns an absolute path. Finding the result relative to the current working directory, without resolving symlinks, would be a little more complicated. I cannot see a use case for relative paths, but if you do, feel free to get in touch.
Metadata
Release files for projdir 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| projdir-0.1.1.tar.gz | 5.2 kB | Without provenance | Details for projdir-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| projdir-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for projdir-0.1.1-py3-none-any.whl |
Total release size: 9.0 kB
Release files / projdir-0.1.1.tar.gz
| Download URL | projdir-0.1.1.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a2cf54e6e2d0323503ce9024009dc33d5905b0746c6a69c2ca1dccccab5e9529
|
|
BLAKE2b-256 checksum How to use checksums |
e0c22137d822331f8b09f6ab03052290ad9a4dc15772e1df70f659a13fb9f0a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
|
Release files / projdir-0.1.1-py3-none-any.whl
| Download URL | projdir-0.1.1-py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d1c5546e9268e3ae9b8b88861de5a3f957b320c9b4eb50452c0bf7a185f95c4a
|
|
BLAKE2b-256 checksum How to use checksums |
b1eb414f1c9d1d6466d439d69189df16f7533fbcf752365600400a30179463cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
|
Security for projdir 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.