将下载的文件保存在文件夹中 (Python)
Save downloaded files in folder (Python)
我有以下代码。内部循环完成(中断)后,我希望将下载的文件放在特定文件夹中。我希望每次执行循环并遇到异常时都执行此操作。有谁知道如何做到这一点?
如有任何帮助,我们将不胜感激!提前致谢!
#Function to export data
def loop_function():
#Search client
searchCustomerButton = driver.find_element_by_xpath('//*[@id="ibSearchPatient"]')
searchCustomerButton.click()
#Loop client ID's
followLoop = range(0, 10)
for x in followLoop:
xpath = '//*[@id="ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00__'
xpath += str(x)
xpath += '"]/td[3]'
#Click on cliënt ID
driver.find_element_by_xpath(xpath).click()
#Click on Zorgtraject
zorgtrajectButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Top_ToolBar_MenuCurrentPage"]/div/ul/li[3]/a/span/span/span')
zorgtrajectButton.click()
#Loop Zorgtraject ID's
followLoop2 = range(0,10)
for x in followLoop2:
try:
xpath2 = '//*[@id="ctl00_CPH_Main_ctl00_RadGrid1_ctl00__'
xpath2 += str(x)
xpath2 += '"]/td[2]'
#Click on Zorgtraject ID
driver.find_element_by_xpath(xpath2).click()
#Dossier button
dossierButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Top_ToolBar_MenuCurrentPage"]/div/ul/li[5]/a/span/span/span')
dossierButton.click()
#Dropdown select
dropdownSelector = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_radGridBehandelVerloop_ctl00_ctl03_ctl01_PageSizeComboBox_Arrow"]')
dropdownSelector.click()
#Prevent not interactable error
time.sleep(1)
#Select 50
selectDropdown = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_radGridBehandelVerloop_ctl00_ctl03_ctl01_PageSizeComboBox_DropDown"]/div/ul/li[4]')
selectDropdown.click()
#Load files to be downloaded
time.sleep(1)
#Check all documents
tickDossier = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_radGridBehandelVerloop_ctl00_ctl02_ctl01_headerChkboxPrint"]')
tickDossier.click()
#Click print
printButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_btnPrintBehandelverloop"]')
printButton.click()
#Click on Zorgtraject ID
zorgtrajectButton2 = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Top_ToolBar_MenuCurrentPage"]/div/ul/li[3]/a/span/span/span')
zorgtrajectButton2.click()
#If no Zorgtraject ID, start function over
except NoSuchElementException:
src = 'C:\Users\sohan\Downloads'
dst = 'C:\Users\sohan\Documents\Werk\BIA Solutions\Huid & Laserkliniek Delft\Data\'
testLoop = range(1, 10)
for i in testLoop:
dst = dst + str(i)
files = [i for i in os.listdir(src) if i.startswith("behandel") and path.isfile(path.join(src, i))]
for f in files:
shutil.move(path.join(src, f), dst)
#Search client
searchCustomerButton = driver.find_element_by_xpath('//*[@id="ibSearchPatient"]')
searchCustomerButton.click()
break
loop_function()
当您使用代码下载文件时
printButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_btnPrintBehandelverloop"]')
printButton.click()`
它将文件下载到您在驱动程序选项中定义的下载文件夹。
假设默认文件夹是 C:/Downloads
并且您想将文件移动到文件夹 C:/Folder1
假设您下载的每个文件都以“my_file”“
”开头
您需要做的是:
import os
from os import path
import shutil
src = `C:/Downloads`
dst = `C:/Folder1`
files = [i for i in os.listdir(src) if i.startswith("my_file") and path.isfile(path.join(src, i))]
for f in files:
shutil.move(path.join(src, f), dst)
我有以下代码。内部循环完成(中断)后,我希望将下载的文件放在特定文件夹中。我希望每次执行循环并遇到异常时都执行此操作。有谁知道如何做到这一点?
如有任何帮助,我们将不胜感激!提前致谢!
#Function to export data
def loop_function():
#Search client
searchCustomerButton = driver.find_element_by_xpath('//*[@id="ibSearchPatient"]')
searchCustomerButton.click()
#Loop client ID's
followLoop = range(0, 10)
for x in followLoop:
xpath = '//*[@id="ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00__'
xpath += str(x)
xpath += '"]/td[3]'
#Click on cliënt ID
driver.find_element_by_xpath(xpath).click()
#Click on Zorgtraject
zorgtrajectButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Top_ToolBar_MenuCurrentPage"]/div/ul/li[3]/a/span/span/span')
zorgtrajectButton.click()
#Loop Zorgtraject ID's
followLoop2 = range(0,10)
for x in followLoop2:
try:
xpath2 = '//*[@id="ctl00_CPH_Main_ctl00_RadGrid1_ctl00__'
xpath2 += str(x)
xpath2 += '"]/td[2]'
#Click on Zorgtraject ID
driver.find_element_by_xpath(xpath2).click()
#Dossier button
dossierButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Top_ToolBar_MenuCurrentPage"]/div/ul/li[5]/a/span/span/span')
dossierButton.click()
#Dropdown select
dropdownSelector = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_radGridBehandelVerloop_ctl00_ctl03_ctl01_PageSizeComboBox_Arrow"]')
dropdownSelector.click()
#Prevent not interactable error
time.sleep(1)
#Select 50
selectDropdown = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_radGridBehandelVerloop_ctl00_ctl03_ctl01_PageSizeComboBox_DropDown"]/div/ul/li[4]')
selectDropdown.click()
#Load files to be downloaded
time.sleep(1)
#Check all documents
tickDossier = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_radGridBehandelVerloop_ctl00_ctl02_ctl01_headerChkboxPrint"]')
tickDossier.click()
#Click print
printButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_btnPrintBehandelverloop"]')
printButton.click()
#Click on Zorgtraject ID
zorgtrajectButton2 = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Top_ToolBar_MenuCurrentPage"]/div/ul/li[3]/a/span/span/span')
zorgtrajectButton2.click()
#If no Zorgtraject ID, start function over
except NoSuchElementException:
src = 'C:\Users\sohan\Downloads'
dst = 'C:\Users\sohan\Documents\Werk\BIA Solutions\Huid & Laserkliniek Delft\Data\'
testLoop = range(1, 10)
for i in testLoop:
dst = dst + str(i)
files = [i for i in os.listdir(src) if i.startswith("behandel") and path.isfile(path.join(src, i))]
for f in files:
shutil.move(path.join(src, f), dst)
#Search client
searchCustomerButton = driver.find_element_by_xpath('//*[@id="ibSearchPatient"]')
searchCustomerButton.click()
break
loop_function()
当您使用代码下载文件时
printButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_btnPrintBehandelverloop"]')
printButton.click()`
它将文件下载到您在驱动程序选项中定义的下载文件夹。
假设默认文件夹是 C:/Downloads
并且您想将文件移动到文件夹 C:/Folder1
假设您下载的每个文件都以“my_file”“
您需要做的是:
import os
from os import path
import shutil
src = `C:/Downloads`
dst = `C:/Folder1`
files = [i for i in os.listdir(src) if i.startswith("my_file") and path.isfile(path.join(src, i))]
for f in files:
shutil.move(path.join(src, f), dst)