Get substring from end to certain character in python [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
How to get only the last part of a path in Python?
8 answers
I want to get the substring from a path from the end to a certain character, take for example the following path:
my_path = "/home/Desktop/file.txt"
My intention is to do something like:
my_path.substring(end,"/")
So I can get the name of the file that is located between the end of the string and the character "/", in this case "file.txt"
python string substring substr
marked as duplicate by Chris_Rands
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 12:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to get only the last part of a path in Python?
8 answers
I want to get the substring from a path from the end to a certain character, take for example the following path:
my_path = "/home/Desktop/file.txt"
My intention is to do something like:
my_path.substring(end,"/")
So I can get the name of the file that is located between the end of the string and the character "/", in this case "file.txt"
python string substring substr
marked as duplicate by Chris_Rands
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 12:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Have you had a look at theos.path
module, specifically theos.path.basename
function?
– Kendas
Nov 16 '18 at 12:30
Usepathlib
.from pathlib import Path; Path("/home/Desktop/file.txt").name == "file.txt"
.
– erip
Nov 16 '18 at 12:33
add a comment |
This question already has an answer here:
How to get only the last part of a path in Python?
8 answers
I want to get the substring from a path from the end to a certain character, take for example the following path:
my_path = "/home/Desktop/file.txt"
My intention is to do something like:
my_path.substring(end,"/")
So I can get the name of the file that is located between the end of the string and the character "/", in this case "file.txt"
python string substring substr
This question already has an answer here:
How to get only the last part of a path in Python?
8 answers
I want to get the substring from a path from the end to a certain character, take for example the following path:
my_path = "/home/Desktop/file.txt"
My intention is to do something like:
my_path.substring(end,"/")
So I can get the name of the file that is located between the end of the string and the character "/", in this case "file.txt"
This question already has an answer here:
How to get only the last part of a path in Python?
8 answers
python string substring substr
python string substring substr
asked Nov 16 '18 at 12:27
L RodMrezL RodMrez
174
174
marked as duplicate by Chris_Rands
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 12:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Chris_Rands
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 12:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Have you had a look at theos.path
module, specifically theos.path.basename
function?
– Kendas
Nov 16 '18 at 12:30
Usepathlib
.from pathlib import Path; Path("/home/Desktop/file.txt").name == "file.txt"
.
– erip
Nov 16 '18 at 12:33
add a comment |
Have you had a look at theos.path
module, specifically theos.path.basename
function?
– Kendas
Nov 16 '18 at 12:30
Usepathlib
.from pathlib import Path; Path("/home/Desktop/file.txt").name == "file.txt"
.
– erip
Nov 16 '18 at 12:33
Have you had a look at the
os.path
module, specifically the os.path.basename
function?– Kendas
Nov 16 '18 at 12:30
Have you had a look at the
os.path
module, specifically the os.path.basename
function?– Kendas
Nov 16 '18 at 12:30
Use
pathlib
. from pathlib import Path; Path("/home/Desktop/file.txt").name == "file.txt"
.– erip
Nov 16 '18 at 12:33
Use
pathlib
. from pathlib import Path; Path("/home/Desktop/file.txt").name == "file.txt"
.– erip
Nov 16 '18 at 12:33
add a comment |
2 Answers
2
active
oldest
votes
The easiest approach, IMHO, would be to split the string:
filename = my_path.split('/')[-1]
add a comment |
use the os.path.basename
for this
In [1]: import os
In [2]: os.path.basename('/home/Desktop/file.txt')
Out[2]: 'file.txt'
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The easiest approach, IMHO, would be to split the string:
filename = my_path.split('/')[-1]
add a comment |
The easiest approach, IMHO, would be to split the string:
filename = my_path.split('/')[-1]
add a comment |
The easiest approach, IMHO, would be to split the string:
filename = my_path.split('/')[-1]
The easiest approach, IMHO, would be to split the string:
filename = my_path.split('/')[-1]
answered Nov 16 '18 at 12:29
MureinikMureinik
187k22141206
187k22141206
add a comment |
add a comment |
use the os.path.basename
for this
In [1]: import os
In [2]: os.path.basename('/home/Desktop/file.txt')
Out[2]: 'file.txt'
add a comment |
use the os.path.basename
for this
In [1]: import os
In [2]: os.path.basename('/home/Desktop/file.txt')
Out[2]: 'file.txt'
add a comment |
use the os.path.basename
for this
In [1]: import os
In [2]: os.path.basename('/home/Desktop/file.txt')
Out[2]: 'file.txt'
use the os.path.basename
for this
In [1]: import os
In [2]: os.path.basename('/home/Desktop/file.txt')
Out[2]: 'file.txt'
answered Nov 16 '18 at 12:30
Albin PaulAlbin Paul
1,536819
1,536819
add a comment |
add a comment |
Have you had a look at the
os.path
module, specifically theos.path.basename
function?– Kendas
Nov 16 '18 at 12:30
Use
pathlib
.from pathlib import Path; Path("/home/Desktop/file.txt").name == "file.txt"
.– erip
Nov 16 '18 at 12:33