我正在尝试计算我保存报告的目录中的文件数。如果文件数不等于X + 1,那么我想重新运行下载报告的代码的中间部分。我必须使用这种方法,因为我正在处理CSV文件,它们按顺序下载(Report(1),Report(2)等)。然后,我将在代码中稍后将这些报告的名称更改为真实姓名。
import time
import os
import os.path
import glob
import shutil
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
files = glob.glob('/Users/me/Desktop/AUTOREPORTS/*')
for f in files:
os.remove(f)
open('/Users/me/Desktop/AUTOREPORTS/report.csv', "w")
for x in range(1, 73):
while True:
try:
fp = webdriver.FirefoxProfile('C:/Users/me/Documents/FirefoxProfile')
browser = webdriver.Firefox(fp)
browser.get('https://website.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("USER")
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)
time.sleep(60)
try:
browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_updateFilters_TitleAnchor").click()
except NoSuchElementException:
pass
time.sleep(140)
browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_CSVButton_ImageAnchor > img").click()
time.sleep(140)
fname = "Report(%s).csv" % (x)
os.chdir('/Users/me/Desktop/AUTOREPORTS')
browser.quit()
#counts number of files in DIR
path = 'C:/Users/me/Desktop/AUTOREPORTS'
count = len([f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))])
print count
#end count of number of files in DIR
except:
browser.quit()
continue
else:
break
这是我得到计数的代码的一部分 - 但是我不确定告诉Python如果这个计数不等于X + 1那么重试(x):
#counts number of files in DIR
path = 'C:/Users/me/Desktop/AUTOREPORTS'
count = len([f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))])
print count
#end count of number of files in DIR
答案 0 :(得分:1)
只需要定期from django.core.exceptions import PermissionDenied
def edit_invoice(request, pk):
invoice = Invoice.objects.get(pk=pk)
if invoice.user != request.user:
raise PermissionDenied
语句即可。也许将中间部分提取到您可以调用的函数中。这样你就不必重写if
语句中的所有代码。
if
此外,使用if count != x + 1:
# Do what you need to download the file again
创建一个可以与try/accept
语句一起使用的函数,而不是在大contextlib
中执行此操作。例如:
with
然后打开你的驱动程序:
from contextlib import contextmanager
@contextmanager
def quitting(thing):
yield thing
thing.close()
thing.quit()
如果出现错误,它会在with quitting(webdriver.Firefox(fp)) as browser:
# Put your code here.
个实例上调用close
和quit
。