【问题标题】:How to resolve FileNotFoundError: [WinError 3] The system cannot find the path specified?如何解决 FileNotFoundError: [WinError 3] 系统找不到指定的路径?
【发布时间】:2023-01-25 18:34:01
【问题描述】:

我问过这个问题,并提供了可能的解决方案,但没有一个有效。如果有人能为我提供解决此问题的方法,我将不胜感激。非常感谢你提前。

尝试重命名 ~/Downloads 文件夹中的文件。

这是代码:

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)

我遇到的错误:

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

【问题讨论】:

    标签: python date time rename


    【解决方案1】:

    它使用完整路径,所以我建议您以编程方式找到 downloads 位置并使用此路径

    current_timestamp = datetime.today().strftime('%d-%b-%Y')
    downloads_path = str(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))
    

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 2020-01-29
      • 2022-01-23
      • 2021-10-13
      • 2021-08-20
      • 2022-12-11
      • 2020-06-03
      • 1970-01-01
      • 2022-11-17
      相关资源
      最近更新 更多