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

时间:2019-05-16 18:46:22

标签: python python-2.7

文件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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

您的文件似乎不存在,或者在当前目录中不存在。 您可以确保文件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