繁体   English   中英

不知道如何修复此文件不存在错误

[英]Don't know how to fix this file doesn't exist error

该文件combo.txt存在,但是当我运行该程序时出现错误。 我尝试多次运行它,但是它给出了相同的错误,我不知道该怎么办。

def failed(email, password):
    pass
def passed(email, password):
    pass
def checker(email, password):
    pass
combos_name = input("please enter combos name: ")
combos = open(combos_name, "r").readLines()
arrange = [lines.replace("\n", "") for lines in combos]
for lines in arrange:
    lines.split(":")
    print(lines)

错误:

C:\Checker\venv\Scripts\python.exe C:/Checker/checker.py
please enter combos name: combo.txt
Traceback (most recent call last):
  File "C:/Checker/checker.py", line 15, in <module>
    combos = open(combos_name, "r").readLines()
FileNotFoundError: [Errno 2] No such file or directory: 'combo.txt'

Process finished with exit code 1

这是一种调用文件对话框以选择文件或键入文件的方法:

from Tkinter import Tk, filedialog

def failed(email, password):
    pass
def passed(email, password):
    pass
def checker(email, password):
    pass
root = Tk()
root.withdraw()

#combos_name = input("please enter combos name: ")

combos_name = filedialog.askopenfilename(initialdir = "./", title = "Select file")

combos = open(combos_name, "r").readlines()
arrange = [lines.replace("\n", "") for lines in combos]
for lines in arrange:
    lines.split(":")
    print(lines)

您的文件似乎不存在,或者在当前目录中不存在。 您可以确保您的文件code.txt与您正在执行的代码位于同一目录中。

若要解决此问题,您可以尝试将abolute路径传递给code.txt文件。

另外,您可以使用os模块检查文件是否存在或检查当前目录。

import os
print(os.path.exists('code.txt'))  # check if code.txt exists
print(os.getcwd())  # check current directory
print(os.listdir())  # list all files in current directory

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM