Skip to main content
refactor for brevity
Source Link
mirekphd
  • 7.2k
  • 4
  • 62
  • 89

@IceAdor's refers to rsplitAs noted by @IceAdor in a comment to @user2902201's solution. rsplit, rsplit is the simplest solution that supportsrobust to multiple periods (by limiting the number of splits to maxsplit of just 1 (from the end of the string)).

Here it is spelt out:

file = 'my.report.txt' print file.rsplit('.', 1maxsplit=1)[0] 

my.report

@IceAdor's refers to rsplit in a comment to @user2902201's solution. rsplit is the simplest solution that supports multiple periods.

Here it is spelt out:

file = 'my.report.txt' print file.rsplit('.', 1)[0] 

my.report

As noted by @IceAdor in a comment to @user2902201's solution, rsplit is the simplest solution robust to multiple periods (by limiting the number of splits to maxsplit of just 1 (from the end of the string)).

Here it is spelt out:

file = 'my.report.txt' print file.rsplit('.', maxsplit=1)[0] 

my.report

Source Link
dlink
  • 1.6k
  • 19
  • 23

@IceAdor's refers to rsplit in a comment to @user2902201's solution. rsplit is the simplest solution that supports multiple periods.

Here it is spelt out:

file = 'my.report.txt' print file.rsplit('.', 1)[0] 

my.report