python - URL 未存储在文件中
问题描述
该 URL 未存储在文件“youtube_alarm_videos.txt”中。代码对我来说似乎很好,但它不起作用。有人可以帮我解决错误吗? 完整代码在这里
""" Alarm Clock
----------------------------------------
"""
import datetime
import os
import time
import random
import webbrowser
import winsound
# If video URL file does not exist, create one
url = print("Enter the link to the video below")
while True:
url_input = input("Enter link:")
break
else:
print("Could not find the link entered. Please check and try again")
if not os.path.isfile("youtube_alarm_videos.txt"):
with open("youtube_alarm_videos.txt", "w") as alarm_file:
alarm_file.write(url_input)
解决方案
我想你需要这样的东西
import datetime
import os
import time
import random
import webbrowser
import winsound
# If video URL file does not exist, create one
def create_file(filename="youtube_alarm_videos.txt"):
# Create new file if doesn't exist
with open(filename, 'w') as e:
pass
url = print("Enter the link to the video below")
while True:
url_input = input("Enter link:")
break
else:
print("Could not find the link entered. Please check and try again")
if os.path.isfile("youtube_alarm_videos.txt"):
create_file()
with open("youtube_alarm_videos.txt", 'w') as e:
e.write(url_input)
推荐阅读
- linux - 在 Linux 中比较 2 个文件夹中每个字节的文件字节并查找重复项
- forms - 如何防止更新 CakePHP 3 beforesave() 中的字段?
- python - 如何根据 urls.py Django 中某个查询参数的值将 URL 路由到单独的视图?
- excel - 运行时错误 91 VBA - 尝试设置工作表但它一直说运行时错误?
- html - 具有自定义设计的侧导航栏
- javascript - 使用父滚动条而不是带有 Angular 虚拟滚动的子组件滚动条
- python - 如何使用 tensorflow 摄取分片 CSV
- docker - Google 容器注册表权限
- javascript - 如何让第二个动画开始,只有在第一个动画完成使用 Javascript 回调之后?
- python - 将 Excel zip 文件内容转换为实际的 Excel 文件?