0

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.

7
  • You are still specifying the non test.pdf Commented Apr 27, 2022 at 20:32
  • 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' -> Commented Apr 27, 2022 at 20:32
  • 1
    FYI there are "raw strings" in Python, where you just add "r" before the first quote in the string, so that you can enter in paths without having to add any escape characters. So it could just look like r"C:\Users\whatever\blah\file.pdf" More info here: docs.python.org/3/reference/… Commented Apr 27, 2022 at 20:32
  • 2
    Question - was the file already renamed? Maybe the original doesn't exist anymore? Commented Apr 27, 2022 at 20:33
  • The original file exists Commented Apr 27, 2022 at 21:23

4 Answers 4

1

I'm pretty sure you ran the code once, renamed the file, and now it won't run again because you already renamed it.

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

2 Comments

No, that did not happen. Confirmed.
Also, I manually went in and deleted ALL files before rerunning each time for test purposes.
1

Careful reading is your friend. Computers don't know or care what you meant:

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'

See the stray r in the path?

4 Comments

I copied the error messages and took out my username for security reasons. I corrected my original post, thank you. Good catch.
Wouldn’t you agree this is very likely a typo. What are the chances the username is actually 'my_username'?
Yes, when I copied my code and replaced my actual username with "my_username" for security purposes, it was indeed a typo.
Fair enough. So you've verified that the path in the error message is exactly correct? E.g. by using it from a Windows prompt to open the file that Python cannot find?
0

Based off the error, I think you are still leaving one the original name or not changing the right one.

import os os.rename( src = "C:\\Users\\my_username\\Desktop\\selenium_downloads\\close_of_day_reports\\test.pdf", dst = "C:\\Users\\my_username\\Desktop\\selenium_downloads\\close_of_day_reports\\close-of-day-2022-04-24-2022-04-23.pdf" ) 

2 Comments

tutorialspoint.com/python/os_rename.htm src = the original file dst = the name of the new file So my order was correct, at least, from what I understand. Am I wrong?
Just for laughs and giggles, I did try switching it though and got the same error.
0

Found the solution:

When you download files using Selenium, you need to put in a sleep method for a few seconds and then you can download/move files without a problem.

Put this in your code after downloading the file, before downloading another:

from time import sleep sleep(10) pass 

You may need to increase the sleep value, but 10 worked for me. The number inside of sleep represents seconds, so sleep(10) means to wait 10 seconds.

1 Comment

Glad you found it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.