Locate a project directory or a config file.
Reason this release was yanked:
Catastrophic bug making the package useless.
Project description
Typical usage
- Place a file named
.marker-file
somewhere in your file system. - From the directory containing the
.marker-file
, or any (nested) subdirectory, you can now use useprojdir
to 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
.git
directory, 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.MarkerNotFound
exception is raised.MarkerNotFound
is 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.
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
projdir-0.1.0.tar.gz
(5.2 kB
view details)
Built Distribution
File details
Details for the file projdir-0.1.0.tar.gz
.
File metadata
- Download URL: projdir-0.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5554934b74882c65aeae0ac5f4f8efb01a11618175a2e68410f6fac1966d306 |
|
MD5 | b9123d0abb68de3eb7fac663fff867e2 |
|
BLAKE2b-256 | 6ad365806e82a4cb4c322cde9cf70ae4b84cdee2c3df1a4c7e51b6650ac9cb9b |
File details
Details for the file projdir-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: projdir-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 094259da2cc5d8d9e7ec6d1ef373f0c40e1bdeed7100198ae65fc687d31b05a6 |
|
MD5 | d72c0585a812e9aea09a051f7892912b |
|
BLAKE2b-256 | 74d3110cfa456616fbe4a8dc5c84c066713b764306fe9b60aed41e4068afc297 |