# Clean way to determine if a file open?

**URL:** <https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 23, 2019, 9:09am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063 "2019-09-23T09:09:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [September 23, 2019, 9:09am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063/1 "2019-09-23T09:09:26Z")

</div>

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?

```julia
        # =====================================
	# 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

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 23, 2019, 9:22am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063/2 "2019-09-23T09:22:48Z")

</div>

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](https://docs.julialang.org/en/v1/manual/style-guide/#Use-naming-conventions-consistent-with-Julia-base/-1) and standard indentation.

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [September 23, 2019, 9:32am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063/3 "2019-09-23T09:32:14Z")

</div>

Thanks Tamas tried _isopen_

`Base.isopen("D:\\Main\\MODELS\\HYDRO\\1D\\PhysicalModel\\HyCatch_1D\\JULIA\\OUTPUT\\Table\\Table_H.csv")`

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

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [September 23, 2019, 9:36am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063/4 "2019-09-23T09:36:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 23, 2019, 9:47am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063/5 "2019-09-23T09:47:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [September 23, 2019, 10:01am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063/6 "2019-09-23T10:01:41Z")

</div>

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 🙄

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

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [September 23, 2019, 10:07am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063/7 "2019-09-23T10:07:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 23, 2019, 10:17am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063/8 "2019-09-23T10:17:31Z")

</div>

> [@Zach\_Christensen](#):
>
> this functionality is unlikely to be standard anywhere

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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 23, 2019, 11:13am UTC](https://discourse.julialang.org/t/clean-way-to-determine-if-a-file-open/29063/9 "2019-09-23T11:13:06Z")

</div>

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 [Networking and Streams · The Julia Language](https://docs.julialang.org/en/v1/manual/networking-and-streams/#Working-with-Files-1)
