PYTHON - UNRAR:我怎样才能建立一个线程来监控下载状态
PYTHON - UNRAR : how can I build a thread to monitoring the downloading status
早上好,
我的脚本需要一些帮助。我是 Python 的初学者,我想知道如何添加线程来验证我的下载过程?
这是我的下载脚本:
class Download:
def __init__(self):
self.path="FolderFiles"
self.target=" /var/www/folder/Output"
def downloadFile(self):
for root, dirs, files in os.walk(self.path, topdown=False):
for name in files:
print(name)
rarFiles=os.path.join(root, name)
unrar = "unrar x -y "+rarFiles+self.target
os.system(unrar)
#time.sleep(10)
附加信息:我使用 python 3.x 和 unrar 库
感谢您的帮助
这可以帮助你,如果我的理解是正确的,你有一堆你想下载的 zip 文件...并检查当前状态,你可以将此打印语句限制为 25 分钟,如果你想要或每 X 数量mb
个。
import requests
url_list = ["http://file-examples.com/wp-content/uploads/2017/02/zip_10MB.zip", "http://file-examples.com/wp-content/uploads/2017/02/zip_10MB.zip", "http://file-examples.com/wp-content/uploads/2017/02/zip_10MB.zip", "http://file-examplesc.com/wp-content/uploads/2017/02/zip_10MB.zipdd", "http://file-examples.com/wp-content/uploads/2017/02/zip_10MB.zip"]
def download_file(url, total_download_mb):
local_filename = url.split('/')[-1]
with requests.get(url, stream=True) as r:
filesize = int(r.headers["Content-Length"]) / 1024 / 1024
downloaded = 0
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
downloaded = (downloaded + len(chunk))
downloaded_mb = downloaded/1024/1024
print("%.2fmb / %.2fmb downloaded." % (downloaded_mb ,filesize))
total_download_mb += downloaded_mb
#download is finished could be unpacked ....
return total_download_mb
def download_url_list(url_list):
total_download_mb = 0
failed_urls = []
for i, url in enumerate(url_list):
try:
total_download_mb = download_file(url, total_download_mb)
print("Total download: %.2fmb" % total_download_mb)
except:
failed_urls.append(url_list[i])
print("failed by file:" + str(i))
print("failed downloads")
print(failed_urls)
download_url_list(url_list)
经过大量测试,我决定采用这种方式:分析下载后的文件大小、下载时间等函数...
unrar库无法提供任何解决方案来测试下载过程中的良好过程
感谢您的帮助
早上好,
我的脚本需要一些帮助。我是 Python 的初学者,我想知道如何添加线程来验证我的下载过程?
这是我的下载脚本:
class Download:
def __init__(self):
self.path="FolderFiles"
self.target=" /var/www/folder/Output"
def downloadFile(self):
for root, dirs, files in os.walk(self.path, topdown=False):
for name in files:
print(name)
rarFiles=os.path.join(root, name)
unrar = "unrar x -y "+rarFiles+self.target
os.system(unrar)
#time.sleep(10)
附加信息:我使用 python 3.x 和 unrar 库
感谢您的帮助
这可以帮助你,如果我的理解是正确的,你有一堆你想下载的 zip 文件...并检查当前状态,你可以将此打印语句限制为 25 分钟,如果你想要或每 X 数量mb
个。
import requests
url_list = ["http://file-examples.com/wp-content/uploads/2017/02/zip_10MB.zip", "http://file-examples.com/wp-content/uploads/2017/02/zip_10MB.zip", "http://file-examples.com/wp-content/uploads/2017/02/zip_10MB.zip", "http://file-examplesc.com/wp-content/uploads/2017/02/zip_10MB.zipdd", "http://file-examples.com/wp-content/uploads/2017/02/zip_10MB.zip"]
def download_file(url, total_download_mb):
local_filename = url.split('/')[-1]
with requests.get(url, stream=True) as r:
filesize = int(r.headers["Content-Length"]) / 1024 / 1024
downloaded = 0
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
downloaded = (downloaded + len(chunk))
downloaded_mb = downloaded/1024/1024
print("%.2fmb / %.2fmb downloaded." % (downloaded_mb ,filesize))
total_download_mb += downloaded_mb
#download is finished could be unpacked ....
return total_download_mb
def download_url_list(url_list):
total_download_mb = 0
failed_urls = []
for i, url in enumerate(url_list):
try:
total_download_mb = download_file(url, total_download_mb)
print("Total download: %.2fmb" % total_download_mb)
except:
failed_urls.append(url_list[i])
print("failed by file:" + str(i))
print("failed downloads")
print(failed_urls)
download_url_list(url_list)
经过大量测试,我决定采用这种方式:分析下载后的文件大小、下载时间等函数... unrar库无法提供任何解决方案来测试下载过程中的良好过程
感谢您的帮助