Reputation: 486
I have a for loop that produces a variable current_out_dir
, sometimes the variable will have a /.
at the end of the line (that is /.$
) I want to replace /.$
with /$
. Currently I have .replace('/.','/')
, but this would replace hidden directories that start with .
as well. e.g. /home/.log/file.txt
I've looked into re.sub()
but I can't figure out how to apply it.
Upvotes: 0
Views: 284
Reputation: 486
The question was about using regex, but I've come up with a more pythonic solution to the problem.
if os.path.split(current_out_dir)[1] == '.':
current_out_dir = os.path.split(current_out_dir)[0]
Upvotes: 0
Reputation: 67968
/\.(?=$)
Try this.This should work for you.This uses a positive lookahead to assert end of string.
Upvotes: 1
Reputation: 174706
Dot will match any character not of newline character. So you need to escape the dot to match a literal dot.
re.sub(r'(?<=/)\.$', r'', string)
Upvotes: 1