Pathlib - join n parents












2














I am playing around with Pathlib and trying to find out if there is an easy way to do the following - I have a group of paths from which I want to extract the first 4 parents, and join these 4 into a path.



Alternatively (if possible) I would like to join all parents up to the parent passed a given one, e.g., c://d1//d2//known//d4//... here I want to extract up to //d4, i.e., the parent just after the 'known' parent.



I know I could just loop over the parts and join up to the nth one, but I am wondering is there a way to do something like the following p.joinpath(p.parents[0:4]), p.joinpath(p.parents[0: 'known_index'+1]), or whatever is the most pythonic.



Update:



I managed to join up to the nth with tuple unpacking print(p.joinpath(*p.parts[0:5])), is there a preferred way and I have still not managed to achieve the goal of the alternative case mentioned above.



Update:



I found an option for the 'Alternative' case print(p.joinpath(*p.parts[0: p.parts.index('PCB_236_237_ARM')+2]))



I am now just looking for the most pythonic ways.










share|improve this question





























    2














    I am playing around with Pathlib and trying to find out if there is an easy way to do the following - I have a group of paths from which I want to extract the first 4 parents, and join these 4 into a path.



    Alternatively (if possible) I would like to join all parents up to the parent passed a given one, e.g., c://d1//d2//known//d4//... here I want to extract up to //d4, i.e., the parent just after the 'known' parent.



    I know I could just loop over the parts and join up to the nth one, but I am wondering is there a way to do something like the following p.joinpath(p.parents[0:4]), p.joinpath(p.parents[0: 'known_index'+1]), or whatever is the most pythonic.



    Update:



    I managed to join up to the nth with tuple unpacking print(p.joinpath(*p.parts[0:5])), is there a preferred way and I have still not managed to achieve the goal of the alternative case mentioned above.



    Update:



    I found an option for the 'Alternative' case print(p.joinpath(*p.parts[0: p.parts.index('PCB_236_237_ARM')+2]))



    I am now just looking for the most pythonic ways.










    share|improve this question



























      2












      2








      2







      I am playing around with Pathlib and trying to find out if there is an easy way to do the following - I have a group of paths from which I want to extract the first 4 parents, and join these 4 into a path.



      Alternatively (if possible) I would like to join all parents up to the parent passed a given one, e.g., c://d1//d2//known//d4//... here I want to extract up to //d4, i.e., the parent just after the 'known' parent.



      I know I could just loop over the parts and join up to the nth one, but I am wondering is there a way to do something like the following p.joinpath(p.parents[0:4]), p.joinpath(p.parents[0: 'known_index'+1]), or whatever is the most pythonic.



      Update:



      I managed to join up to the nth with tuple unpacking print(p.joinpath(*p.parts[0:5])), is there a preferred way and I have still not managed to achieve the goal of the alternative case mentioned above.



      Update:



      I found an option for the 'Alternative' case print(p.joinpath(*p.parts[0: p.parts.index('PCB_236_237_ARM')+2]))



      I am now just looking for the most pythonic ways.










      share|improve this question















      I am playing around with Pathlib and trying to find out if there is an easy way to do the following - I have a group of paths from which I want to extract the first 4 parents, and join these 4 into a path.



      Alternatively (if possible) I would like to join all parents up to the parent passed a given one, e.g., c://d1//d2//known//d4//... here I want to extract up to //d4, i.e., the parent just after the 'known' parent.



      I know I could just loop over the parts and join up to the nth one, but I am wondering is there a way to do something like the following p.joinpath(p.parents[0:4]), p.joinpath(p.parents[0: 'known_index'+1]), or whatever is the most pythonic.



      Update:



      I managed to join up to the nth with tuple unpacking print(p.joinpath(*p.parts[0:5])), is there a preferred way and I have still not managed to achieve the goal of the alternative case mentioned above.



      Update:



      I found an option for the 'Alternative' case print(p.joinpath(*p.parts[0: p.parts.index('PCB_236_237_ARM')+2]))



      I am now just looking for the most pythonic ways.







      python python-3.x path pathlib






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 31 '18 at 8:56

























      asked May 31 '18 at 8:46









      10SecTom

      763515




      763515
























          1 Answer
          1






          active

          oldest

          votes


















          1














          This looks pythonic enough to me:



          p1 = pl.Path('c://d1//d2//known//d4//')
          idx = p1.parts.index('known')
          p2 = pl.Path(*p1.parts[:idx+1])


          I use pl.Path(*segments) to join the segments because the instance method p.joinpath() appends the segments to the instance's p own path.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50620190%2fpathlib-join-n-parents%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            This looks pythonic enough to me:



            p1 = pl.Path('c://d1//d2//known//d4//')
            idx = p1.parts.index('known')
            p2 = pl.Path(*p1.parts[:idx+1])


            I use pl.Path(*segments) to join the segments because the instance method p.joinpath() appends the segments to the instance's p own path.






            share|improve this answer


























              1














              This looks pythonic enough to me:



              p1 = pl.Path('c://d1//d2//known//d4//')
              idx = p1.parts.index('known')
              p2 = pl.Path(*p1.parts[:idx+1])


              I use pl.Path(*segments) to join the segments because the instance method p.joinpath() appends the segments to the instance's p own path.






              share|improve this answer
























                1












                1








                1






                This looks pythonic enough to me:



                p1 = pl.Path('c://d1//d2//known//d4//')
                idx = p1.parts.index('known')
                p2 = pl.Path(*p1.parts[:idx+1])


                I use pl.Path(*segments) to join the segments because the instance method p.joinpath() appends the segments to the instance's p own path.






                share|improve this answer












                This looks pythonic enough to me:



                p1 = pl.Path('c://d1//d2//known//d4//')
                idx = p1.parts.index('known')
                p2 = pl.Path(*p1.parts[:idx+1])


                I use pl.Path(*segments) to join the segments because the instance method p.joinpath() appends the segments to the instance's p own path.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 '18 at 1:05









                normanius

                1,4421027




                1,4421027






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50620190%2fpathlib-join-n-parents%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation