Before running a program we want to determine if the file we want to write to is open?
I am questioning if there is a cleaner way to interrogate if a file is open?
# =====================================
# CHECK IF FILE IS OPEN
# =====================================
function CHECK_IFOPEN(Path)
try
if isfile(Path) # If the file is there than delete it before saving figure
rm(Path)
end
return
catch
error("\n HyCatch ERROR: File open please close file before running = : ", Path, "\n \n")
end
end # function CHECK_IFOPEN
ERROR: MethodError: no method matching isopen(::String)
Closest candidates are:
isopen(::Mmap.Anonymous) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\Mmap\src\Mmap.jl:41
isopen(::Base.Filesystem.File) at filesystem.jl:88
isopen(::Base.BufferStream) at stream.jl:1151
Type ? isopen and you’ll see the doc string for isopen. As @Tamas_Papp said, it works on I/O streams. You’re using a string. You need to open it as a stream first and then check if it is open.
With “file open” you probably mean if the file is in use and locked by another process.
As far as I know there is typically no such method to just check this except for trying to open the file and check the error or exception (as you do in your code).
C# e.g. does not provide just checking for in use.
Linux itself has lsof which provides a list of open files but not a isFileOpen or similar.
And no such thing in Windows PowerShell.
All solutions try to open a file instead of just checking.
The reason is, again ASFAIK , is that this check would not help: If you check and get a “is not open, you can use the file”, you can still fail to open the file, because in the mean time another process could still put a lock on by open it.
Maybe I’m misunderstanding, but I believe the point of @oheil’s comment was that this functionality is unlikely to be standard anywhere because it would amount to searching all executing processes and checking them activity on this particular file.
This.
Because it is useless as inbetween checking and opening the file, another process can always lock the file again.
And if your own process is controling the opening and closing of a file, you can easily implement the check of the status on your own, e.g. by using streams in julia as mentioned above.