【问题标题】:Issue with File Not Found找不到文件的问题
【发布时间】:2021-10-08 19:25:25
【问题描述】:

我只是从一个我认为我以前没有遇到过问题的文件中绘图时遇到了问题。我不确定是不是因为我从 PyCharm 切换到了 IDLE

这是我当前的代码:

import time
import os

keep_running = True
last_time = 0
file = os.path.abspath(r'C:\Users\AUser\Desktop\test.txt')
current_time = os.path.getmtime(file)

print(file)

这是我得到的输出:

Traceback (most recent call last):
  File "C:/Users/AUser/Desktop/Scripts/FileAlert.py", line 9, in <module>
    current_time = os.path.getmtime(file)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\genericpath.py", line 55, in getmtime
    return os.stat(filename).st_mtime
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\AUser\\Desktop\\test.txt'

如果我从文件路径中删除“r”,则会收到 unicode 错误。该文件与脚本位于不同的目录中,因此我不确定问题所在。 Windows 10 机器上的 Python 3.9 会发生这种情况。

【问题讨论】:

  • 我的建议是使用 / 而不是 \。并验证您是否正确设置了路径。
  • 该文件存在吗?似乎没有。你可以尝试import os 然后os.listdir('C:\\Users\\AUser\\Desktop') 来获取目录列表。
  • @EllisThompson - 我认为这不会有所作为。 C:\\Users\\AUser\\Desktop\\test.txt 不存在。
  • @tdelaney 那么错误是正确的吗?如果文件不存在,那么这是预期的输出。
  • @EllisThompson - 对。错误消息显示了使用 python 的双反斜杠转义为单个反斜杠技术的路径。撤消该操作时,消息会显示 C:\Users\AUser\Desktop\test.txt 不存在。海报应该牢记这一点!

标签: python filenotfoundexception


【解决方案1】:

在这种情况下,问题在于被搜索的文件在技术上被命名为“test.txt.txt”。

使用这个建议帮助我找到了这个错误:

该文件存在吗?似乎没有。您可以尝试 import os 然后 os.listdir('C:\Users\AUser\Desktop') 来获取目录列表。来自用户@tdelaney

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 2012-02-29
    • 1970-01-01
    • 2013-09-18
    • 2013-07-03
    • 2020-11-28
    • 2012-05-09
    相关资源
    最近更新 更多