Clean way to determine if a file open?

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?

        # =====================================
	# =====================================
	function CHECK_IFOPEN(Path)
			if isfile(Path) # If the file is there than delete it before saving figure
			error("\n HyCatch ERROR: File open please close file before running = : ", Path, "\n \n")
	end # function CHECK_IFOPEN

Above, you are just checking that a there is a file at Path.

Maybe you are looking for isopen, but that works for I/O streams associated with files.

Also, please consider following common naming conventions and standard indentation.

Thanks Tamas tried isopen


But get the following error:

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 :grin:, 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.

You are correct I mean if the file is in use and locked by another process. I came to your conclusion that JULIA does not have yet this functionality :roll_eyes:

It will be nice to have this functionality in JULIA in future release.

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.

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.

I think is a red herring in this discussion. They original problem may just be a conceptual misunderstanding of how file I/O works in general.

@JosephPollacco, I would recommend reading