文本文件无法使用open()打开

时间:2019-11-10 03:52:22

标签: python

在这个练习中,我需要帮助。我必须询问用户是否要为特定日期或一周中的某些天设置闹钟。除了打印时间加1之外,我还必须说是白天还是晚上(代码的最后一部分是由课程的老师制作的,我做了所有警报)。

该代码应该打开一个.txt文件,但是没有,我已经运行了几次该代码,并检查了Pycharm,但是什么也没有。在这里

from time import sleep
import datetime

NIGHT_STARTS = 19
DAY_STARTS = 8
HOUR_DURATION = 1


def write_file_and_screen(text, file_name):
    with open(file_name, "a+") as file_text:
        file_text.write("{}{}".format(text, "\n"))
        print(text)


def week_date_to_day(day):
    days_list = {0: "Monday", 1: "Tuesday", 2: "Wednesday", 3: "Thursday", 4: "Friday", 5: "Saturday",
                 6: "Sunday"}
    day_weekday = day.weekday
    if day_weekday == days_list.keys():
        week_day_of_the_day = days_list.get(day_weekday)
        return week_day_of_the_day


def main():
    today = datetime.date.today()
    current_time = datetime.datetime.now()
    is_night = False

    want_alarm = input("Do you want to set a alarm? Yes/No ")
    if want_alarm == "Yes":

        # Aquí preguntamos si desea una alarma para una fecha específica.

        specific_date = input("Do you want an alarm in a specific date? Yes/No ")
        if specific_date == "Yes":
            date_user = input("Tell me the date. (dd/mm/yyyy) ")
            date_format = datetime.datetime.strptime(date_user, "%d/%m/%Y")
            if datetime.date.today() == date_format:
                write_file_and_screen("ALARM. It's {}".format(date_format), "Specific alarm.txt")
                print("ALARM. It's {}".format(date_format))

        elif specific_date == "No":
            # Aquí preguntamos si desea una alarma normal, haciendo que elija el día y la hora.

            normal_alarm = input("Do you want a normal alarm? Yes/No ")
            if normal_alarm == "Yes":
                hour_alarm = int(input("Hour of the alarm? 0/23 "))
                datetime.time(hour=hour_alarm)
                days_of_alarm_input = ""
                days_of_alarm_list = []
                print("Write End to end the loop ")
                while not days_of_alarm_input == "End":
                    days_of_alarm_input = input("Tell me the days that you want to set the alarm 0 to 6, 0 is "
                                                "Monday ""and 6 is Sunday ")
                    days_of_alarm_list.append(days_of_alarm_input)
                if days_of_alarm_input == "End":
                    for i in days_of_alarm_list:
                        if today.weekday() == days_of_alarm_list:
                            write_file_and_screen("ALARM. It's {}".format(week_date_to_day(today)), "Weekdays "
                                                                                                    "alarm.txt")
    while True:        # Se imprime la hora actual y se le va sumando una más, además de que si indica si es de día
                        # o de noche

        sleep(HOUR_DURATION)
        current_time += datetime.timedelta(hours=1)
        light_changed = False

        if (current_time.hour >= NIGHT_STARTS or current_time.hour <= DAY_STARTS) and not is_night:
            is_night = True
            light_changed = True

        elif (DAY_STARTS < current_time.hour < NIGHT_STARTS) and is_night:
            is_night = False
            light_changed = True

        if light_changed:
            if is_night:
                write_file_and_screen("It's night", "hours.txt")
            else:
                write_file_and_screen("It's day", "hours.txt")

        write_file_and_screen("The hour is {}".format(current_time), "horas.txt")
        sleep(2)


if __name__ == "__main__":
    main()

当我运行该程序并输入警报所需的数据时,只需将该程序转到代码的第三部分,并开始打印时间并在不打开.txt文件的情况下加1:

The hour is 2019-11-09 19:50:51.614472
The hour is 2019-11-09 20:50:51.614472
The hour is 2019-11-09 21:50:51.614472
The hour is 2019-11-09 22:50:51.614472
The hour is 2019-11-09 23:50:51.614472
The hour is 2019-11-10 00:50:51.614472
The hour is 2019-11-10 01:50:51.614472
The hour is 2019-11-10 02:50:51.614472
The hour is 2019-11-10 03:50:51.614472
The hour is 2019-11-10 04:50:51.614472
The hour is 2019-11-10 05:50:51.614472

如果您需要了解其他信息,或者如果我不能很好地解释自己,请告诉我。 (对不起,我的英语)


更新:

特定日期警报起作用!谢谢!

但是还有另一个问题。 “正常警报”没有。程序在for i in days_of_alarm_list:行时,将其跳过到while True i = 6 , it suppose to pass the for i in days_of_alarm_list: But the program passes to while True

2 个答案:

答案 0 :(得分:0)

之前有类型不匹配的情况,这就是为什么您每次都要转到第三部分。

datetime.date.today()返回
'datetime.date(2019, 11, 10)'
if返回if datetime.date.today() == date_format.date() :

将您的dogs条件替换为以下行:

cats

答案 1 :(得分:0)

您的代码运行正常,我在计算机上对其进行了测试,

在您的代码中,每次都会使您的状况处于休眠状态1个小时,这就是为什么您的代码需要花费更多时间来执行文件创建,修改的原因。 条件循环时,每次恰好等待1小时

睡眠方法是

sleep(HOUR_DURATION)

您可以通过将持续时间从sleep(HOUR_DURATION)更改为sleep(5)

进行检查

您的状况只会睡五毫秒

愉快的生活