Skip to main content

A small library for getting current path data for a script that imports this library.

Project description

current_path

badge

current_path is a small library for getting current path data for a script that imports this library.

Installation

pip install current-path

Usage

Get the current file's path which imports current-path:

from current_path import current_file

if __name__ == '__main__':
    print(current_file())

Get the current directory's path which contains the file which imports current-path:

from current_path import current_dir

if __name__ == '__main__':
    print(current_dir())

Temporarily change the current working directory to the file's directory until the code block under the context manager has been completed:

from current_path import current_dir_as_cwd
import os


def create_file(path: str):
    with open(path, 'w'):
        """File has been created."""

        
if __name__ == '__main__':
    #  CWD is some arbitrary directory.
    
    with current_dir_as_cwd():
        #  CWD is this file's directory.
        os.mkdir("file_directory")
        create_file("file_0.txt")
    
    #  CWD returns to same arbitrary directory.

The above code ensures that, no matter which working directory the script is invoked from, the file is created in the same location.

License

Licensed under the CC0 License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

current-path-0.0.4.tar.gz (5.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page