Detecting if a file is open by another application
37 次查看(过去 30 天)
显示 更早的评论
Hi! I'm using MATLAB R2011b on Windows XP SP3; is there a way to determine if a file is open by another application (if not within matlab then perhaps w/ a DOS command)? (From within my matlab ap, I'm calling another, non-matlab application which produces a file; I want my matlab ap to do a few things while it's waiting for this other ap to finish, then open up that produced file and append some stuff to it. I'm thinking if there's some way to detect that the file is still open, I can just while loop until that's not true, then open it and proceed...)
Thanks!
0 个评论
采纳的回答
Walter Roberson
2011-11-4
Not easily from within MATLAB. See http://www.mathworks.com/matlabcentral/answers/1030-is-there-a-way-to-detect-if-a-file-is-open-in-another-application-from-matlab
A typical mechanism in MS Windows is to simply try the fopen(), in a try/catch block, under the assumption that the file is local or on a Microsoft shared file system (not an NFS file system!), and under the assumption that the application that opened the file for writing used the default open() settings that lock the file when write permission is requested. This method is unreliable and can suffer from "race conditions", but you might find it "good enough" for you.
7 个评论
Walter Roberson
2011-11-4
I am going to go out on a limb and guess that the text being matched against does not include drive letters.
更多回答(3 个)
Image Analyst
2011-11-4
Not that I know of, other than to keep calling dir() on it and checking the date stamp, or trying to open it by looping until you don't have an exception thrown (in the event that some program put a lock on that file, which not every program does).
Daniel Shub
2011-11-4
Since you wrote the other app that opens the file, there are lots of ways of doing it (none of them are particularly good). I think the best answer depends on how much you want to modify the other app, what language that app is written in, and how tightly coupled the other app and your MATLAB app are.
3 个评论
Walter Roberson
2011-11-4
Python documentation for open() and os.open() is silent on this issue, which is not surprising as Microsoft's own documentation is silent on this issue.
http://docs.python.org/library/functions.html#open
http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx
http://packages.python.org/lockfile/lockfile.html
But notice that the above package is for advisory locks -- mandatory locks are not generally implemented in Unix.
One thing I can say with certainty is that using 'a+' mode is *not* a mechanism for preventing or denying shared access: the '+' indicators have entirely different purposes.
Mike Tinston
2017-10-30
In a Linux based system you can try:
[stat, struct] = system(sprintf('lsof %s', filename));
If struct is empty, not process has that file open. I've read that "netstat -b" is the windows equivalent. (https://www.reddit.com/r/sysadmin/comments/1kzrrs/windows_equivalent_of_lsof_i/)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!