Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python enum to hold file paths

Tags:

python

path

enums

I have to deal with a bunch of folder/file paths in my project. As I use them multiple times in my project, I chose to store them in variables. So, if a need to change a path, I only need to change the variable value.

I am currently storing the pathlib.Path objects in an enum, like the example below.

from pathlib import Path
from enum import Enum

class Paths(Enum):
    CONTENTS = Path('path/to/contents/')

    FOLDER_IN_CONTENTS = CONTENTS / 'some_folder'

    IMAGES = Path('path/to/images')

    FILE_IN_IMAGES = IMAGES / 'some_file'

Something tells me that this is not a good use for an Enum Class, though I can't really explain why (maybe because I have never seen something like this). If so, what would be the best way to store these file/folder paths?

like image 792
Gustavo Rangel Avatar asked May 17 '26 14:05

Gustavo Rangel


1 Answers

Good practice would be storing the paths in a config file. Then you don't have to change any code when any of the paths change, which is good if you intend to share your project. YAML is commonly used for that purpose.

There's nothing wrong per se with your solution though.

like image 194
hasleron Avatar answered May 19 '26 02:05

hasleron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!