How to get file path without extension in Rust?
Asked Answered
C

2

34

Please help me to find an elegant way to get file path without extension; cut off file extension from path or something.

Coition answered 26/1, 2016 at 5:25 Comment(0)
S
47

Check the Path::file_stem method. You can find an example there. It works at least from Rust 1.6.

Slotnick answered 26/1, 2016 at 5:27 Comment(3)
Is there a method which gets the full absolute path before extension, not just the filename?Crowell
do you know the bahaviour if the file name has two extensions like "file.abc.xyz"?Musketeer
@AndrewMackenzie the linked docs answer this: it returns "the portion of the file name before the final ."Phineas
E
22

Using the Path::with_extension("") method gives you the full path with a possible file extension removed.

Eventual answered 17/1, 2021 at 17:34 Comment(1)
This correctly answers the question, as if the input is a path with intermediate directories, this returns the full path, with the file_stem.Musketeer

© 2022 - 2024 — McMap. All rights reserved.