Algorithms
알람 맞추기, 컴퓨터종료, 퇴근하자! [파이썬]
엄청큰노란닭
2023. 3. 8. 11:20
업무 중 심심해서 알람을 만들었다.
그냥 주파수로 알려주는 코드이다.
import time
import winsound
# 17시59분이 되면 알람 울리기
while True:
now = time.localtime()
if now.tm_hour == 17 and now.tm_min == 59:
print("알람 울립니다!")
winsound.Beep(440, 1000) # 440 Hz 주파수로 1초 동안 소리 울리기
break
else:
time.sleep(60) # 1분 대기 후 다시 검사 ctrl + c로 종료
심심해서 파이썬으로 알람을 맞춰보았다.
진짜로 되는지 궁금해서 켜봤다가 깜짝놀랐다.
다음은 url을 열어서 노래를 틀어준다. 주의사항!! 크롬으로 열어둔 사이트는 다 꺼져버린다... 마치 암살자?
import time
import webbrowser
import os
# 17시59분이 되면 알람 울리기
while True:
now = time.localtime()
if now.tm_hour == 17 and now.tm_min == 59:
print("알람 울립니다!")
url = "https://www.youtube.com/watch?v=lqFby1rSGyI"
webbrowser.open(url)
break
else:
time.sleep(100)
print("알람 다시 울립니다!")
위 코드는 url을 열어주기만해서 시끄러웠다.
이 코드는 url을 다시 닫아주는 코드이다. while문을 두번 알람이 울리게 바꿔 놓았다.
주석처리 한것은 컴퓨터까지 꺼주는 기능이니 필요할 때만 사용하도록 하자.
import time
import webbrowser
import os
while True:
now = time.localtime()
if now.tm_hour == 11 and now.tm_min == 53:
print("알람 울립니다!")
url = "https://www.youtube.com/watch?v=lqFby1rSGyI"
webbrowser.open(url)
time.sleep(30)
os.system("taskkill /im chrome.exe /f")
time.sleep(60) # 1분 대기
url = "https://www.youtube.com/watch?v=lqFby1rSGyI"
webbrowser.open(url)
time.sleep(30)
break
# os.system("shutdown /s /t 1" 컴퓨터 종료
이제 코드를 하나로 합쳐보겠습니다. 다들 퇴근하세요
import calendar
import datetime
import time
import webbrowser
import os
today = calendar.datetime.date.today()
print(calendar.month(today.year, today.month))
now = datetime.datetime.now()
print("현재 시간은 ", now.strftime('%Y-%m-%d %H:%M'))
while True:
now = time.localtime()
if now.tm_hour == 18 and now.tm_min == 00:
print("알람 울립니다!")
url = "https://youtu.be/xbO1CY1PXkE"
webbrowser.open(url)
time.sleep(30)
os.system("taskkill /im chrome.exe /f")
time.sleep(60) # 1분 대기
print("마지막 알람입니다!")
url = "https://youtu.be/xbO1CY1PXkE"
webbrowser.open(url)
now = datetime.datetime.now()
print("현재 시간은 ", now.strftime('%Y-%m-%d %H:%M'))
time.sleep(30)
print("컴퓨터 종료합니다!")
os.system("shutdown /s /t 1")
break
진짜 너무 아쉬워서 진짜최종 찐찐찐 막 으로 하나 더 수정했습니다.
종료 전 모든 프로세서를 종료하고 30초 카운트 하는 코드입니다.
어제 수정 안된거 켜놨더니 틱 꺼져버리네요,,
import calendar
import datetime
import time
import webbrowser
import os
today = calendar.datetime.date.today()
print(calendar.month(today.year, today.month))
now = datetime.datetime.now()
print("현재 시간은", now.strftime('%Y-%m-%d %H:%M'), '입니다.')
while True:
now = time.localtime()
if now.tm_hour == 18 and now.tm_min == 2:
print("알람 울립니다!")
url = "https://youtu.be/xbO1CY1PXkE"
webbrowser.open(url)
time.sleep(30)
os.system("taskkill /im chrome.exe /f")
time.sleep(60) # 1분 대기
print("마지막 알람입니다!")
url = "https://youtu.be/xbO1CY1PXkE"
webbrowser.open(url)
now = datetime.datetime.now()
print("현재 시간은", now.strftime('%Y-%m-%d %H:%M'), '입니다.')
time.sleep(30)
print("시스템 종료합니다!")
os.system('taskkill /f /im explorer.exe') # 시스템 종료
os.system('taskkill /f /im MicrosoftEdge.exe') # 시스템 종료
os.system('taskkill /f /im chrome.exe') # 시스템 종료
print("30초 후 컴퓨터 종료합니다!")
print("현재 시간은", now.strftime('%Y-%m-%d %H:%M'), "오늘도 고생하셨습니다.")
cnt = 30
while cnt >= 0:
cnt -= 1
time.sleep(1)
print(cnt,'초 후 컴퓨터를 종료합니다.')
if cnt == 0:
os.system('shutdown /s /t 0') # 컴퓨터 종료
설명은 주석을 달았습니다.