首页 > 解决方案 > Errno2,找不到文件(即使我将文件放在与脚本相同的文件夹中)

问题描述

我是一名试图成为一名狙击手的初学者,我有一个要打开的文件。即使我将它放在同一个文件夹中,我也会不断收到未找到文件的错误。这是代码:

    accounts = open("accounts.txt", "r")
    accounts = f.read()

它返回此错误:

File "c:/users/(my username)/AXESNIPE/lib/AXESNIPE.py", line 73, in <module> 
accounts = open("accounts.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'accounts.txt'

这是我的文件所在的位置:</p>

在此处输入图像描述

如果有人可以提供帮助,那将是非常酷的 idk 为什么会发生这种情况,我花了很长时间在这上面,好的,谢谢再见

标签: pythonfile-not-found

解决方案


  1. f是什么?你是说accounts.read()我猜
  2. 我想你没有添加完整的文件路径。如果您的文件是您的脚本所在的位置,那么您可能必须添加文件的完整路径或在 sep env 中运行。

尝试这个 :

import os

file = "c:/users/(my username)/AXESNIPE/lib/accounts.txt" # your file path here.
if os.path.exists(file):
    accounts = open(file, "r")
    accounts = accounts.read()
else:
    print(f"No Files Found Matching This Name :: {file}")

推荐阅读