Python script creates folders, how to reference path- absolute vs relative?

Viewed 39

I have a parent folder named A, which contains my Python script. The script creates a sub folder, also within A.

I'm unsure how I should reference the path when creating the new sub folder because:

  1. I can't use absolute path because each user will have their username as part of the path to the Git repo.

  2. I'm worried about using relative path because I don't know from which folder users will execute the script.

How is this typically handled?

1 Answers

use __file__ to get the path to the python script you are in, regardless of where the script is run from.

from pathlib import Path
script_path_dir = Path(__file__).parent
sub_folder = script_path_dir / 'your_subfolder'
Related