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;
}







-1
















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"










share|improve this question













marked as duplicate by Chris_Rands python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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


















-1
















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"










share|improve this question













marked as duplicate by Chris_Rands python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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














-1












-1








-1









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"










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 12:27









L RodMrezL RodMrez

174




174




marked as duplicate by Chris_Rands python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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



















  • 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

















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












2 Answers
2






active

oldest

votes


















1














The easiest approach, IMHO, would be to split the string:



filename = my_path.split('/')[-1]





share|improve this answer































    1














    use the os.path.basename for this



    In [1]: import os

    In [2]: os.path.basename('/home/Desktop/file.txt')
    Out[2]: 'file.txt'





    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      The easiest approach, IMHO, would be to split the string:



      filename = my_path.split('/')[-1]





      share|improve this answer




























        1














        The easiest approach, IMHO, would be to split the string:



        filename = my_path.split('/')[-1]





        share|improve this answer


























          1












          1








          1







          The easiest approach, IMHO, would be to split the string:



          filename = my_path.split('/')[-1]





          share|improve this answer













          The easiest approach, IMHO, would be to split the string:



          filename = my_path.split('/')[-1]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 12:29









          MureinikMureinik

          187k22141206




          187k22141206

























              1














              use the os.path.basename for this



              In [1]: import os

              In [2]: os.path.basename('/home/Desktop/file.txt')
              Out[2]: 'file.txt'





              share|improve this answer




























                1














                use the os.path.basename for this



                In [1]: import os

                In [2]: os.path.basename('/home/Desktop/file.txt')
                Out[2]: 'file.txt'





                share|improve this answer


























                  1












                  1








                  1







                  use the os.path.basename for this



                  In [1]: import os

                  In [2]: os.path.basename('/home/Desktop/file.txt')
                  Out[2]: 'file.txt'





                  share|improve this answer













                  use the os.path.basename for this



                  In [1]: import os

                  In [2]: os.path.basename('/home/Desktop/file.txt')
                  Out[2]: 'file.txt'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 12:30









                  Albin PaulAlbin Paul

                  1,536819




                  1,536819















                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine