如何让python检测何时保存/关闭某个文件?

问题描述 投票:0回答:1

我制作了这个程序,使用户可以输入他们想要下载的内容。然后,它会打开浏览器和YouTube,其中包含用户输入的关键字。用户可以将尽可能多的URL复制并粘贴到文本文件url.txt。只需保存文本文件,在python shell中按Enter,即可开始下载。 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')
python subprocess detect python-webbrowser pytube
1个回答
0
投票
import hashlib import time def get_file(file): with file: return file.read() hash_string = "" last_hash_string = "" while True: hash_string = hashlib.md5(get_file(open("c:\\txt.txt", 'rb'))).hexdigest() if hash_string != last_hash_string and (len(hash_string) > 0 and len(last_hash_string) > 0): print("Houve alteração...") else: print(".") last_hash_string = hash_string time.sleep(1)
© www.soinside.com 2019 - 2024. All rights reserved.