【问题标题】:No such file or directory? [duplicate]无此文件或目录? [复制]
【发布时间】:2021-03-28 18:40:33
【问题描述】:

我遇到了“FileNotFoundError: [Errno 2] No such file or directory: 'Pronunciation_Data.txt'”的问题

我不断将我的工作目录重置到正确的位置,但它似乎不想保留(使用 Spyder/Anaconda)。我还尝试了以下方法:

file = open(r'C:\path\to\Pronunciation_Data.txt\Users\stephaniecheetham\Desktop\Thesis')
import os
os.chdir(r'C:\Users\stephaniecheetham\Desktop\Thesis')
file = open('Pronunciation_Data.txt')

file = open("Pronunciation_Data.txt",'r')

file = open("<Users/stephaniecheetham/Desktop/Thesis>\Pronunciation_Data.txt",'r')

file = open("C:/stephaniecheetham/Desktop/Thesis/Pronunciation_Data.txt",'r')

他们中的任何一个都不走运。只是同样的错误。我最近也遇到了一个模块问题,并使用import os 命令修复了它。

有什么建议吗?

【问题讨论】:

标签: python errno


【解决方案1】:

您的目录存在吗?通过执行以下操作检查您的文件是否存在:

import os

os.chdir('C:\Users\stephaniecheetham\Desktop\Thesis')

try {
  file = open('Prononunciation_Data.txt')
} except FileNotFoundError {
  print("File not found")
}

您的目录存在吗?使用os.listdir()检查目录是否存在

它们是您的代码中的 2 个错误:

  • 您尚未导入os 模块
  • 它们是第 2 行中的 SyntaxError,其中缺少 1 个括号

【讨论】:

  • 我怀疑她的真实代码有这些错误,否则她不会得到 FileNotFoundError。
【解决方案2】:

为了帮助调试,您还可以执行 os.listdir() 来查看您是否确实在正确的目录中。

此外,还不清楚文件的完整路径应该是什么。 C:/stephaniecheetham 不同于 C:/Users/stephaniecheetham

【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2021-12-26
    • 2020-07-28
    • 2020-04-18
    • 2016-07-01
    • 2018-05-28
    • 2021-12-19
    相关资源
    最近更新 更多