首页 > 解决方案 > 为什么我的 python 脚本在 IDLE 和从 CMD 执行而不是双击时工作

问题描述

我正在努力调试它,但恐怕它超出了我目前的理解。

我在 python 中创建了一个简单的脚本,其目的是将字符串作为输入(从谷歌表格复制粘贴)并将其转换为适合上传到谷歌日历的 csv 文件。它是硬编码的,以适应我们工作中轮班计划的格式。可以说示例性输入可以是:MSLS-1Comp-offES-1..ES-22。然后程序根据指定的规则解析输入,将解析的项目打印为列表,最后以正确的格式创建一个 csv 文件。

这是问题所在:该程序在我尝试的任何地方都可以完美运行,除非我尝试通过在桌面上双击它来运行它(Windows10)。从 CMD、IDLE 或在线运行时,程序可以正常工作并产生预期的结果而不会出现任何错误。我写的其他简单脚本,从桌面运行没问题。在这个特定的程序中,程序打开(与所有其他程序一样,在 python3.9 中),但在特定的行中退出(同样,当我从 cmd、IDLE 或在线运行该程序时,它不会发生)。通过插入打印功能,我设法找到了程序“崩溃”的地方。在我看来,由于某种原因,程序退出/崩溃就行了with io.open("shifts.csv", "w", encoding="utf-8-sig") as f:

与代码有关吗?那为什么只使用命令提示符在同一系统上运行时运行正常?这与我在计算机上设置 python 的方式有关吗?在这种情况下,为什么我编写的其他脚本可以从桌面完美运行?我在这里真的很困惑。我将非常感谢任何建议。谢谢!

这是完整的代码:

import subprocess
import sys
import io

year = "2021"
month = input("Please enter month in number format (e.g., '07' for June):\n")

shifts = input("Please paste your shifts:\n")

word = ''
shifts_list = []
for char in shifts: # iterate through the string
    word = word + char # add the character to the word
    if(char in ['1','2','3','.']): # check if the character at current iteration is one of the characters in the list
        shifts_list.append(word) # add the word formed by now to the list
        word = '' # re-initialize the word variable to an empty string so as to start the next word
    if (char in ['S'] and word[-2] in ['M']):
        shifts_list.append(word) # add the word formed by now to the list
        word = '' # re-initialize the word variable to an empty string so as to start the next word

print(shifts_list)


headers = "Subject, Start Date, Start Time, End Date, End Time, Description, Private\n"
start_times = ["6:30", "7:30", "6:30", "9:00", "10:00"]
end_times = ["15:00", "16:00", "17:00", "17:30", "18:30"]
subjects = ["Early ", "Office ", "10h ", "Middle ", "Late "]


print(headers)
input("after this point the program exits")
with io.open("shifts.csv", "w", encoding="utf-8-sig") as f:
    f.write(headers)

    for day, shift in enumerate(shifts_list):
        description = shift
        date = str(day+1) + "." + month + "." + year
        if shift == "Int-ES1":
            print(subjects[0] + ', ' + date +', ' + start_times[0] + ', ' + date + ', ' + end_times[0] + ', ' + description + ', ' + "TRUE\n")
            f.write(subjects[0] + ', ' + date +', ' + start_times[0] + ', ' + date + ', ' + end_times[0] + ', ' + description + ', ' + "TRUE\n")

        if shift == "Int-ES2":
            print(subjects[1] + ', ' + date +', ' + start_times[1] + ', ' + date + ', ' + end_times[1] + ', ' + description + ', ' + "TRUE\n")
            f.write(subjects[1] + ', ' + date +', ' + start_times[1] + ', ' + date + ', ' + end_times[1] + ', ' + description + ', ' + "TRUE\n")

        if shift == "IntES3":
            print(subjects[2] + ', ' + date +', ' + start_times[2] + ', ' + date + ', ' + end_times[2] + ', ' + description + ', ' + "TRUE\n")
            f.write(subjects[2] + ', ' + date +', ' + start_times[2] + ', ' + date + ', ' + end_times[2] + ', ' + description + ', ' + "TRUE\n")

        if shift == "MS":
            print(subjects[3] + ', ' + date +', ' + start_times[3] + ', ' + date + ', ' + end_times[3] + ', ' + description + ', ' + "TRUE\n")
            f.write(subjects[3] + ', ' + date +', ' + start_times[3] + ', ' + date + ', ' + end_times[3] + ', ' + description + ', ' + "TRUE\n")

        if shift == "Int-LS1":
            print(subjects[4] + ', ' + date +', ' + start_times[4] + ', ' + date + ', ' + end_times[4] + ', ' + description + ', ' + "TRUE\n")
            f.write(subjects[4] + ', ' + date +', ' + start_times[4] + ', ' + date + ', ' + end_times[4] + ', ' + description + ', ' + "TRUE\n")


print("Your shifts have been successfully saved to a CSV file. You can now import it to your calendar :)")
input("Press enter to exit...")

def open_folder(path):
    if sys.platform == 'darwin':
        subprocess.check_call(['open', '--', path])
    elif sys.platform == 'linux2':
        subprocess.check_call(['gnome-open', '--', path])
    elif sys.platform == 'win64':
        subprocess.check_call(['explorer', path])

open_folder(path=".")  # open current directory

标签: pythoncsvcrashexecution

解决方案


推荐阅读