1

I want to replace the following characters tmp from between the /tmp/ to the current date from this string /tmp/tmpy2919ufy. The output should be something like this /20200615/tmpy2919ufy Could you please give some ideas as to how I might be able to do this? Thanks!

1
  • have a look at this - is that what you need? the current date as string is simply datetime.now().strftime('%Y%m%d'), see the docs Commented Jun 15, 2020 at 11:37

1 Answer 1

1

You can simply do this with datetime

from datetime import datetime

string = "/tmp/tmpy2919ufy"
replace_string = datetime.today().strftime('%Y%m%d')

string = string.replace('tmp',replace_string,1)

#output >>> '/20200615/tmpy2919ufy' 

Sign up to request clarification or add additional context in comments.

2 Comments

you should note that this only changes the string, not the directory name. also, you do not need regex here. a simple .replace('tmp', replace_string, 1) will do.
Thanks a lot, it definetly helps! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.