python - 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 为什么会发生这种情况,我花了很长时间在这上面,好的,谢谢再见
解决方案
- f是什么?你是说
accounts.read()
我猜 - 我想你没有添加完整的文件路径。如果您的文件是您的脚本所在的位置,那么您可能必须添加文件的完整路径或在 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}")
推荐阅读
- sql - SQL 子查询和列命名问题
- python - 组合 2 个数据帧以生成一个包含重复值的文件
- python - 将不同格式的json转换为csv
- react-native - 格式数字功能仅在我调试 React Native Expo 时才有效
- sql - 合并或插入 db2 的忽略
- angular - 在模板中传递动态 formControl 值
- typescript - 为什么我们需要 Rxjs 中的 Tap 运算符
- .net - 我可以在同一个项目中运行 .net core 2.2 和 .net core 3.0
- java - 从 firebase 数据库中删除节点
- r - 将一定数量的行传递给R中的函数