为什么我的 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",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] + ',' + "TRUE\n")

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

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

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

        if shift == "Int-LS1":
            print(subjects[4] + ',' + start_times[4] + ',' + end_times[4] + ',' + "TRUE\n")
            f.write(subjects[4] + ',' + "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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)