1

我制作了这个程序,让用户输入他们想要下载的内容。然后,它会使用用户输入的关键字打开浏览器和 YouTube。用户可以将尽可能多的 URL 复制并粘贴到文本文件url.txt中。只需保存文本文件,在 python shell 中按回车,即可开始下载。python有没有办法检测某个文件被关闭或保存,这样我就不必按Enter开始下载了?

import webbrowser
import pytube
from subprocess import call

key = input('Input what you want to download: ')
a_website = "https://www.youtube.com/results?search_query="+key
webbrowser.open_new(a_website)

open('url.txt', 'w')
call(['notepad', 'url.txt'])

start = input('Press enter when ready to download >>> ')
with open('url.txt','r') as f:
    urls = f.readlines()
number = len(urls)
num = 0
for url in urls:
    num += 1
    print('Downloading video',num,'of',number)
    yt = pytube.YouTube(url)
    stream = yt.streams.first()
    stream.download()

open('url.txt', 'w')
4

1 回答 1

0

这是看门狗检测的示例:

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
于 2020-06-10T03:46:50.947 回答