简体   繁体   English

当我将名称从文本文件加载到 Combobox 时,为什么我的 function 会出现 OSError?

[英]Why do I get an OSError with my function when I load names into a Combobox from a text file?

def clock_in(): #  triggered with a button in tkinter
    employee = employee_drop_list.get() # get name selected from Combobox
    employee_drop_list.delete(0, tk.END) # delete name after button clicked

    working = "Employee Time Records\\Employees_Working.txt" # folder & file
    date = datetime.datetime.now().strftime("%Y-%m-%d, %I:%M %p") # date format
    filename = datetime.datetime.now().strftime("Week_%V_%Y") # file name prefix
    location = "Employee Time Records" # folder in same location as code
    connected = os.path.join(location, filename + "_" + employee + ".txt")

    Employee_List = "Employee Time Records\\Employee_List.txt" # list to check

    if employee not in open(working).read() and employee in open(Employee_List).read():
        with open(connected, "a") as file:
            file.write(employee + "_" + date + "_" + "Clocked In" + "\n")
            label['text'] = login_success()
            with open(working, "a") as t:
                t.write(employee + "\n")
    else:
        label['text'] = login_fail()

Here is the Code for how I am loading names into the Combobox. The error goes away and works perfectly if I load values to the Combobox into the code instead of loading from the text file and I don't understand why.这是我如何将名称加载到 Combobox 的代码。如果我将 Combobox 的值加载到代码中而不是从文本文件中加载,错误就会消失并完美运行,我不明白为什么。

employees = []
with open("Employee Time Records\\Employee_List.txt") as listFile:
    employees = [line for line in listFile]

employee_drop_list = ttk.Combobox(frame, font=20, state="normal")
employee_drop_list['values'] = list(employees)
employee_drop_list.place(relwidth=.5, relx=.125, height=60)

The error I get is我得到的错误是

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\rorym\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "E:/Programming/PYTHON FOR WINDOWS/tkinter/MT Systems employee time clock/text_test/TimeClock.py", line 161, in <lambda>
    clock_in_button = ttk.Button(frame, text="Clock In", command=lambda: clock_in())
  File "E:/Programming/PYTHON FOR WINDOWS/tkinter/MT Systems employee time clock/text_test/TimeClock.py", line 47, in clock_in
    with open(connected, "a") as file:
OSError: [Errno 22] Invalid argument: 'Employee Time Records\\Week_07_2021_Amador Espinoza\n.txt'

To fix this issue I just use strip() to remove the spaces.要解决此问题,我只需使用 strip() 删除空格。

with open("Employee Time Records\\Employee_List.txt", "r") as listFile:
    employees = listFile.read().split("\n")

employee_drop_list = ttk.Combobox(frame, font=20, state="normal")
employee_drop_list['values'] = tuple(employees)
employee_drop_list.place(relwidth=.5, relx=.125, height=60)
    

This could be for two reasons.这可能有两个原因。 One, it may be that the folder is not findable by your code.一,可能是您的代码找不到该文件夹。 Make sure it's in the same directory as the.py file.确保它与 .py 文件位于同一目录中。 Second, it could be because you didn't specify a mode.其次,可能是因为您没有指定模式。 Try adding , "r" to the end of the open call.尝试将, "r"添加到open调用的末尾。

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

相关问题 为什么我会得到这个 OSError? - Why do I get this OSError? 运行此脚本时为什么会出现“ OSError:[Errno 2] No such file or directory”的错误? - Why do I get “OSError: [Errno 2] No such file or directory” when running this script? 为什么在运行“mult(num)”function 时得到 0? - Why do I get 0 when running my "mult(num)" function? 我如何将 json 文件名(不带扩展名)加载到列表框和组合框(python)中? - how do i load json file names (without extension) into a listbox and into a combobox (python)? 为什么在将文本写入文件时会得到括号? - Why do I get a parentheses when writing text to a file? 为什么我的函数中出现UnboundLocalError? - Why do I get an UnboundLocalError in my function? 为什么我的 function 会出现这个 IndentationError? - why do i get this IndentationError in my function? 如何使用部分文件名作为函数的输入? - How do I use part of my file names as input for a function? 当我尝试在jupyter笔记本中加载CSV文件时,为什么会出现nameerror - why do i get a nameerror when i try to load a CSV-File in jupyter notebook 为什么我在运行程序时输入的前两个输入会在文本文件中重复? - Why do the first two inputs I enter when I run my program repeat in the text file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM