36

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

2 Answers 2

47

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

3
  • 9
    Is there a method which gets the full absolute path before extension, not just the filename?
    – ditoslav
    Commented Dec 9, 2020 at 15:07
  • do you know the bahaviour if the file name has two extensions like "file.abc.xyz"? Commented Jul 5, 2023 at 22:03
  • @AndrewMackenzie the linked docs answer this: it returns "the portion of the file name before the final ."
    – Kyle Carow
    Commented Oct 20, 2023 at 18:53
23

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

1
  • 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. Commented Nov 30, 2022 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.