Pathlib - join n parents
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
add a comment |
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
add a comment |
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
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
python python-3.x path pathlib
edited May 31 '18 at 8:56
asked May 31 '18 at 8:46
10SecTom
763515
763515
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 12 '18 at 1:05
normanius
1,4421027
1,4421027
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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