I am trying to rename a file, but python cannot find the file specified.
I have a file located here:
C:\Users\my_username\Desktop\selenium_downloads\close_of_day_reports\close-of-day-2022-04-24-2022-04-23.pdf I am trying to rename the file to test.pdf
Here is the code I am using:
import os os.rename( src = "C:\\Users\\my_username\\Desktop\\selenium_downloads\\close_of_day_reports\\close-of-day-2022-04-24-2022-04-23.pdf", dst = "C:\\Users\\my_username\\Desktop\\selenium_downloads\\close_of_day_reports\\test.pdf" ) The error message I am getting is:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\my_username\\Desktop\\selenium_downloads\\close_of_day_reports\\close-of-day-2022-04-24-2022-04-23.pdf' -> 'C:\\Users\\my_username\\Desktop\\selenium_downloads\\close_of_day_reports\\test.pdf' What am I doing wrong?
Edit #1:
- The original file was not deleted, it still exists.
- It's really strange, when I run it the first time, the file does not get renamed, but when I run it again, it does.
- Weird, for some reason it works in Python Shell, but not my Python file.
Edit #2:
I am using Selenium to download the file. When I comment the part of my code out that downloads the file from Selenium, my os.rename code works fine. Weird.
python FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\my_usernamer\\Desktop\\selenium_downloads\\close_of_day_reports\\close-of-day-2022-04-24-2022-04-23.pdf' ->r"C:\Users\whatever\blah\file.pdf"More info here: docs.python.org/3/reference/…