Nathan123
Nathan123

Reputation: 763

how to pause an R script until a file has been downloaded?

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?

Upvotes: 2

Views: 967

Answers (1)

Nathan123
Nathan123

Reputation: 763

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")

Upvotes: 2

Related Questions