python - 文本文件不会用 open() 打开
问题描述
我需要这个练习的帮助。我必须询问用户是否要为特定日期或一周中的某些天设置警报。除了打印时间加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 ,它假设在 days_of_alarm_list 中传递 for i :
但是程序传递到 while True
解决方案
你的代码工作正常,我在我的机器上测试过,
在您的代码中,您每次都会睡 1 小时,这就是为什么您的代码需要更多时间来执行文件创建、修改。每次 while 条件循环时都等待 1 小时
睡眠方法是
sleep(HOUR_DURATION)
您可以通过将持续时间从更改为来检查sleep(HOUR_DURATION)
它sleep(5)
你的情况只会睡五毫秒
快乐编码
推荐阅读
- android - 如何在库模块中使用不同的包名称进行调试和发布?
- javascript - 将 html 表格行中断到下一行以转换为 pdf
- c# - 从列表中移除和 API 的使用
- c# - ListView/GridView 未扩展以填充可用空间
- jquery - 在段落标签中设置最大字符数限制
- c# - 我想通过 C# 中的 selenium 读取文本并将其存储在变量中
- java - 带有伴随对象的 Kotlin 枚举类与 Firebase 数据库的字段集成
- c - 驱动程序函数不会在 c 中执行,但其他函数会
- c# - 使用 lamda 表达式构建实体的代码如何工作
- php - Wordpress 使用 add_filter() 使用 the_content 复制内容