3

I would like to ask how can I check a file's status (whether it is opened or closed) before reading in R programming?

I have tried myfile = open(filenames); it doesn't work.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user3003009
  • 31
  • 1
  • 2

3 Answers3

6

I think the trick is to use open = "w" to open for writing. This seems to work perfectly for me:

file.opened <- function(path) {
  suppressWarnings(
    "try-error" %in% class(
      try(file(path, 
               open = "w"), 
          silent = TRUE
      )
    )
  )
}

After I open an Excel file:

file.opened("C:\\Folder\\tbl1.xlsx")
# TRUE

And when I close it:

file.opened("C:\\Folder\\tbl1.xlsx")
# FALSE
MS Berends
  • 4,489
  • 1
  • 40
  • 53
  • 2
    When I run this function, it corrupts the Excel file. Sometimes I'll still be able to open the file after one or two uses, but the problem happens later if I keep using it. Trying to open it with Excel gives "The file format or file extension is not valid," and using openxlsx::read.xlsx() gives: "Error in file(con, "r") : invalid 'description' argument In addition: Warning message: In unzip(xlsxFile, exdir = xmlDir) : error 1 in extracting from zip file" – NotReallyHere12 Dec 12 '20 at 09:31
  • That's strange, I never got this problem. Do you get this with any Excel file? – MS Berends Dec 12 '20 at 20:22
  • Yes, even empty files that I've just created. I'm using Excel 365. – NotReallyHere12 Dec 13 '20 at 02:27
  • Beware: Currently this code always corrupts files when checked. Maybe it's a version issue. I almost lost very crucial data. – Delta._.43 Nov 28 '22 at 10:33
  • `file(path, open = "w")` seems to overwrite the existing file with a blank file. `file(path, open = "r+")` still tries to open the file for writing, but doesn't actually write anything to it (ie. leaves the existing data there). See the modes section in `?file` – vorpal Apr 13 '23 at 06:42
0

When you use the function file a connection to the file is returned:

con <- file(description="path/to/file", ...)

You can check to see if this is open using the isOpen function, which will return a logical (TRUE, FALSE, or NA).

If you don't have a connection in your R session, or the connection has been deleted, then the connection is closed:

An excerpt of ?file:

 ‘close’ closes and destroys a connection.  This will happen
 automatically in due course (with a warning) if there is no longer
 an R object referring to the connection.

File connections can be opened and closed using open and close respectively.

Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
  • Basically you want to use `file` not `open` :) – Scott Ritchie Nov 18 '13 at 01:54
  • 2
    I tried with a MS-Word doc and it didn't work. `isOpen` returns `FALSE`, whether the doc is open or not. – Dan Chaltiel Nov 16 '17 at 11:11
  • @DanChaltiel `isOpen` does not check whether a file is open in any application, it checks whether a specific connection is open in R, e.g. in the case above, the `con` variable. – Scott Ritchie Nov 16 '17 at 22:41
  • 2
    In general you can't check whether a file is currently open in another program. See https://stackoverflow.com/questions/1951791/how-to-check-if-a-file-is-already-open-by-another-process-in-c for some discussion on this – Scott Ritchie Nov 16 '17 at 22:43
0

For Excel files, I used the following approach and it works in my computer : rename the Excel file with it's name. If the Excel file is opened, it returns FALSE. If the Excel file is closed, it returns TRUE.

file.rename(from = "C:/Users/xxx/Desktop/Test file access/file1.xlsx",
            to = "C:/Users/xxx/Desktop/Test file access/file1.xlsx")
Emmanuel Hamel
  • 1,769
  • 7
  • 19