如何让python脚本等到下载完成
Posted
技术标签:
【中文标题】如何让python脚本等到下载完成【英文标题】:How to make a python Script wait until download has finished 【发布时间】:2017-11-25 19:22:49 【问题描述】:我的脚本下载一个压缩文件,提取相关部分替换文件和文件夹等。它曾经完美地工作,由于某种原因它现在决定停止工作并且只下载部分压缩文件,当然,因为压缩文件是不完整我收到一个错误,说下载的文件不是 zipfile。我的脚本如下。
def downloadupdate():
xbmcgui.Dialog().ok(
"[B][COLOR white]Daily Updater[/B][/COLOR]",
"Favourites and some software will now update",
"Elements of your system will be cleaned",
"Daily Update will take at most 2 minutes")
#ONLY HAVE THE SUPER FAVOURITES FOLDER IN THE ZIPFILE!!
url = 'http://x.com/x/x/Super Favourites.zip'
destination = xbmc.translatePath('special://home/userdata/addon_data/iupdatefix/Super Favourites.zip')
favzip = urllib.urlopen(url)
xbmc.executebuiltin("Notification(Downloading new updates, PLEASE WAIT,()")
with open(xbmc.translatePath('special://home/userdata/addon_data/iupdatefix/Super Favourites.zip'), "wb") as zipFile:
zipFile.write(favzip.read())
xbmc.executebuiltin("Notification(Download Complete, Please wait,()")
time.sleep(5)
xbmc.executebuiltin("Notification(Updating Click and Play channels, Please wait,()")
updatezip = xbmc.translatePath('special://home/userdata/addon_data/iupdatefix/Super Favourites.zip')
extractupdate = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/')
oldfav = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/Super Favourites')
yeszip = os.path.exists(updatezip)
time.sleep(5)
if yeszip:
xbmc.executebuiltin("Notification(Removing previous, Please wait,()")
shutil.rmtree(oldfav, ignore_errors=False)
xbmc.executebuiltin("Notification(Updating, now,()")
gh = open(updatezip, 'rb')
zp = zipfile.ZipFile(gh)
zp.extractall(extractupdate)
gh.close()
time.sleep(3)
xbmc.executebuiltin("Notification(updated, Now Checking sd,()")
# put this command at the end of whole script --->>>os.remove(updatezip)
else:
xbmc.executebuiltin("Notification(Update file Corrupt, Please try again,()")
【问题讨论】:
favzip.read()
等待下载。但是,如果您收到错误,则问题出在连接上,您可能必须从 beginnig 开始下载。使用try/except
furas 你能解释一下我在这个例子中如何使用它,因为我想尝试一下。
Python 文档:8. Errors and Exceptions
【参考方案1】:
问题已经解决了,不是代码。它是托管服务,我不得不重置服务器上的文件权限,然后一切都再次完美运行。
【讨论】:
以上是关于如何让python脚本等到下载完成的主要内容,如果未能解决你的问题,请参考以下文章
如何让python 脚本在指定的时间内运行,如在10点,让一个python脚本自动跑