-2

I tried to name the file or rename a file in python, but it is showing a invalid syntax at the end of the path, before ',r'

os.rename(r  'D:\\Stackoverflow\\SC.png'  ,r 'D:\\Stackoverflow\\Screen'+str(localtime)+'.png')
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Here's a similar question: https://stackoverflow.com/questions/2953834/windows-path-in-python. The answer is, you should probably use regular forward slashes - and then you'll never ever have to deal with this kind of errors again – ForceBru Nov 20 '19 at 20:42
  • Thanks for your comment. Now I came to know more about the escape sequences in Python. Once again thanks for your support. – Jeyasuriya Natarajan Nov 20 '19 at 21:10

1 Answers1

-1

Try this:

from datetime import datetime 
import os

src = "/tmp/test.txt"
dst = "/tmp/python_" + str(datetime.now().strftime('%Y-%m-%d-%H_%M_%S')) +".inf" 

os.rename(src, dst)