Home > Blockchain >  How to resolve FileNotFoundError: [WinError 3] The system cannot find the path specified?
How to resolve FileNotFoundError: [WinError 3] The system cannot find the path specified?

Time:01-26

I have asked this question and was provided possible solutions but none of them work. I would really appreciate if someone can provide me the solution to this problem. Thank you so much in advance.

Trying to rename a file in ~/Downloads folder.

Here is the code:

import os
from datetime import datetime

# adding date-time to the file name
current_timestamp = datetime.today().strftime('%d-%b-%Y')
old_name = r"/Downloads/Test.xlsx"
new_name = r"/Downloads/Test_"   current_timestamp   ".xlsx"
os.rename(old_name, new_name)

Error that I'm having:

FileNotFoundError: [WinError 3] The system cannot find the path specified: '/Downloads/Test.xlsx' -> '/Downloads/Test_25-Jan-2023.xlsx'

CodePudding user response:

It's working with full path, so I suggest you locate downloads location programmatically and use this path

import os
from datetime import datetime
from pathlib import Path

current_timestamp = datetime.today().strftime('%d-%b-%Y')
downloads_path = os.path.join(Path.home(), 'Downloads')
old_name = 'Test.xlsx'
new_name = f'Test_{current_timestamp}.xlsx'
os.rename(os.path.join(downloads_path, old_name), os.path.join(downloads_path, new_name))
  • Related