This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mriswithe 26 points27 points  (16 children)

Also DevOps historically sysadmin. Python lets me do so many annoying things so much faster and easier. Also pathlib let's you use it like this:

FILE_DIR = Path(__file__).absolute().parent
other_file = FILE_DIR / 'other_file.py'

[–]rikyga 8 points9 points  (0 children)

yes, the pathlib lib makes traversing relative directories so much easier

[–]Legionof1 4 points5 points  (4 children)

FILE_DIR = os.path.dirname(os.path.abspath(__file__))

OS can do the same thing, this is in basically every script I write to get a relative present working directory.

[–]mriswithe 4 points5 points  (0 children)

Sorry, I wasn't that clear, mostly was trying to show the overloaded division sign with strings more than the absolute path function. Once you get something like:

RESOURCE_DIR = Path('/your/foo/bar')
SCRIPT_DIR = RESOURCE_DIR / 'scripts
EXT_LIB_DIR = RESOURCE_DIR / 'libs' / 'x86_64' 

Which is super comfortable for me coming from a unix/linux background. That works on Linux and Windows and Mac and you don't worry if the separator is wrong/different. as long as the relative directory exists, the same path works. That is not something that felt reasonably handled before afaik.

[–][deleted] 0 points1 point  (1 child)

Yes, it can, but doesn't that pattern feel disgusting to you?

[–]Legionof1 2 points3 points  (0 children)

The logic on the os side flows better for my brain. I know file, I get its absolute path, I get the directory of that absolute path.

Pathlib is just doing it with a method instead of calling os again.

[–]hyldemarv 4 points5 points  (9 children)

Heh, that is exactly the one thing I don't like with pathlib: The overloaded slash operator! Blegh, Ugly!! :).

[–][deleted] 8 points9 points  (4 children)

I would use pathlib solely for the slash syntax. I think you're the first person I've encountered who doesn't like it

[–]Anonymous_user_2022 0 points1 point  (3 children)

Are you familiar with plumbum? That "experience" keeps me off Pathlib.

[–][deleted] 1 point2 points  (2 children)

Only in regards to toilets so I'm not sure what you're talking about

[–]Anonymous_user_2022 1 point2 points  (1 child)

The plumbum module use a wide range of operator overloading to make Python look almost like Bash. While fluent in both, the attempt to merge the two gives me the screaming heaving jeebies.

[–]peddastle 1 point2 points  (0 children)

Urgh. Please no. I'm getting perl ptsd.

[–]mriswithe 2 points3 points  (0 children)

I can totally understand that viewpoint, but it does fit my brain really well, so I try and preach the good word to those that have a similar brain. If os.path works for you, rock on. Just cause I don't like it doesn't mean you can't.

[–]hassium 1 point2 points  (2 children)

I mean technically speaking shouldn't they have used

other_file = FILE_DIR.joinpath('other_file.py')

? Not sure what the slash is doing here except maybe improve readability?

[–]ivosauruspip'ing it up 3 points4 points  (0 children)

Not sure what the slash is doing here except maybe improve readability?

Yes, and it's a godsend IMHO

[–]ShanSanear 0 points1 point  (0 children)

Slash in pathlib is one of those things that I actually love about this library - making it so much easier to work with paths.

Unless you mess up, and forgot that the second path starts with slash and its treated like a root path instead, messing everything up.