2

I have the following piece of code which runs a python selenium script that downloads a report

library(reticulate)
source_python("C:/Users/Gunathilakel/Desktop/Dashboard Project/Dashboard/PBK-Report-Automation-for-Dashboard-master/pbk automation.py") 

I want to make sure that R waits until the earlier piece of code has downloaded a file into my downloads folder before the script executes this next piece of code

my.file.copy <- function(from, to) {
   todir <- dirname(to)
   if (!isTRUE(file.info(todir)$isdir)) dir.create(todir, recursive=TRUE)
   file.copy(from = from,  to = to,overwrite = TRUE)
 }

 my.file.copy(from = "C:/Users/Gunathilakel/Downloads/Issued and Referral Charge.csv",
                to = "C:/Users/Gunathilakel/Desktop/Dashboard Project/Dashboard/RawData/Issued and Referral Charge2020.csv")

I found this question How to make execution pause, sleep, wait for X seconds in R?

But is it possible to wait for execution until a file has been downloaded?

2
  • 1
    Change your Python script to do a synchronous download Commented Jun 10, 2020 at 21:30
  • Seconding @HongOoi's comment as the best answer. Commented Jun 11, 2020 at 3:33

1 Answer 1

2

actually found a solution from this question

Wait for file to exist

library(reticulate)
source_python("C:/Users/Gunathilakel/Desktop/Dashboard Project/Dashboard/PBK-Report-Automation-for-Dashboard-master/pbk automation.py") 

while (!file.exists("C:/Users/Gunathilakel/Downloads/Issued and Referral Charge.csv")) {
  Sys.sleep(1)
}


 # Moves Downloaded CSV file of Issued_and_refused from Downloads --------
 my.file.copy <- function(from, to) {
   todir <- dirname(to)
   if (!isTRUE(file.info(todir)$isdir)) dir.create(todir, recursive=TRUE)
   file.copy(from = from,  to = to,overwrite = TRUE)
 }

 my.file.copy(from = "C:/Users/Gunathilakel/Downloads/Issued and Referral Charge.csv",
                to = "C:/Users/Gunathilakel/Desktop/Dashboard Project/Dashboard/RawData/Issued and Referral Charge2020.csv")
Sign up to request clarification or add additional context in comments.

4 Comments

Just beware that if that file doesn't end up where you expect, then you will have an infinite loop. It might be worth adding an escape hatch, or better yet change the Python script to do a synchronous download as suggested by @HongOoi.
@dpritch I am using selenium to download the file using a command like the following driver.find_element(By.XPATH,'//*[@id="ctl00_ContentPlaceHolder1_rvReports_ctl09_ctl04_ctl00_Menu"]/div[7]/a').click() # This clicks on the csv file to be downloaded can you share any resources on how to do a synchronous download inside a python script.
According to this it appears that in the general case there is no way to wait until the download is completed and what you have proposed is probably the best recourse. In some cases though, you might be able to extract the target URL using Selenium, in which case you could perform the download using e.g. the requests package which would give you much greater control over the download attempt.
can also look for file size with for loop and sleep. If same then break loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.