0

So I am trying to rename a file that has been downloaded to my downloads folder.

My code:

import os
from time import strftime

current_time = strftime("(%x-%Xp)")

old_name = r'C:/Users/name/Downloads/file.pdf'
new_name = r'C:/Users/name/Downloads/file'+current_time+'.pdf'
os.rename(old_name, new_name)

However, I keep geting this error:

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/Users/darianmoore/Downloads/file.pdf' -> 'C:/Users/darianmoore/Downloads/file(12/31/19-10:47:47AM).pdf'

I'm very confused because if I use this code it works fine, but it's not the formatting I want:

import os
from time import strftime

current_time = strftime("(%m%d%y-%I%M%p)")

old_name = r'C:/Users/name/Downloads/file.pdf'
new_name = r'C:/Users/name/Downloads/file'+current_time+'.pdf'
os.rename(old_name, new_name)
Darian Moore
  • 33
  • 1
  • 6

1 Answers1

4

File name cannot contain a slash. file(12/31/19-10:47:47AM).pdf does.

Marat
  • 15,215
  • 2
  • 39
  • 48