gpt4 book ai didi

python - 在继续脚本之前验证文件已下载

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:03 25 4
gpt4 key购买 nike

我需要确保文件已下载,然后我的脚本才能继续。我已经对exists()函数做了一些研究,但是我找不到我实际上想要做的事情的例子。

我正在尝试下载多个文件。我有一个用于下载所有文件的静态目的地。我需要确保文件已成功下载到文件,然后脚本才能继续。有人可以帮我举一个该脚本的示例吗?

这是我正在处理的内容:

import time
import os
import glob
import os.path
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import MoveTargetOutOfBoundsException
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException

for x in range(1, 100):
while True:
try:
fp = webdriver.FirefoxProfile('C:/Users/me/Documents/FirefoxProfile')
browser = webdriver.Firefox(fp)
browser.get('https://reportcenter.com')

time.sleep(8)

browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").clear()
browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").send_keys("usr")
browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").clear()
browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").send_keys("pwd")
browser.find_element_by_id("ctl00_PlaceHolderMain_login_login").click()

#gets user to reporting front end

ReportMgr= browser.find_element_by_partial_link_text('Report Manager')
ReportMgr.click()

time.sleep(5)

CustomReport= browser.find_element_by_partial_link_text('Custom Report')
CustomReport.click()

time.sleep(5)

ProgramManagement= browser.find_element_by_partial_link_text('Program Management')
ProgramManagement.click()
ProgramManagement= browser.find_element_by_partial_link_text('Program Management').send_keys(Keys.ARROW_LEFT)

#pulls reports

browser.find_element_by_partial_link_text('Program Management').click()
time.sleep(60)
browser.find_element_by_partial_link_text('Program Management').send_keys(Keys.ARROW_DOWN * x, Keys.ENTER, Keys.ENTER)
time.sleep(180)
browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_CSVButton_ImageAnchor > img").click()
time.sleep(180)
##THIS IS WHERE I NEED TO VERIFY THAT THE REPORT HAS DOWNLOADED BEFORE I CAN CONTINUE
browser.find_element_by_partial_link_text('Program Management').click()
time.sleep(60)
browser.quit()

except:
browser.quit()
continue
else:
break

最佳答案

一种方法是等待新文件出现在目标文件夹中。

使用示例:

# take a snapshot of the folder
waiter = FileWaiter(r"C:\Downloads\*.pdf")

# trigger the download
browser.find_element_by_css_selector("...").click()

# wait for a new file or timeout after 10 seconds
new_file = waiter.wait_new_file(10)

# display the new file
print new_file

等待类(class):

import os, time, glob, numbers, exceptions 

class FileWaiter:

def __init__(self, path):
self.path = path
self.files = set(glob.glob(path))

def wait_new_file(self, timeout):
"""
Waits for a new file to be created and returns the new file path.
"""
endtime = time.time() + timeout
while True:
diff_files = set(glob.glob(self.path)) - self.files
if diff_files :
new_file = diff_files.pop()
try:
os.rename(new_file, new_file)
self.files = set(glob.glob(self.path))
return new_file
except :
pass
if time.time() > endtime:
raise Exception("Timeout while waiting for a new file in %s" % self.path)
time.sleep(0.1)

关于python - 在继续脚本之前验证文件已下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35923749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com