Re: Check file is locked?

2009-07-13 Thread Lawrence D'Oliveiro
In message 652cca82-44a3-473f-b640-
c2336a9cf...@v15g2000prn.googlegroups.com, Rajat wrote:

 ... my whole idea is to close the wordpad / notepad application so that I
 can delete the file and the directory where this file resides.

Don't you think the user might have that application open for a reason?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-09 Thread Rajat
On Jul 8, 12:45 pm, Tim Golden m...@timgolden.me.uk wrote:
 Rajat wrote:
  On Jul 8, 4:57 am, Lawrence D'Oliveiro l...@geek-
  central.gen.new_zealand wrote:
  In message mailman.2795.1246997268.8015.python-l...@python.org, Christian

  Heimes wrote:
  By the way most operating systems don't lock a file when it's opened for
  reading or writing or even executed.
  The general conclusion seems to be that mandatory locking is more trouble
  than it's worth.

  My OS is a windows XP sp3. All I'm trying to achieve is to close an
  application ( which could be a notepad, a wordpad or some other text
  editor) that have opened my file. Once the file is closed I can easily
  delete that file.

  I guess, if file is opened by one of that application, the file must
  be locked and so is the reason I cannot delete the file.

 I assume that your real requirement is: I can't open/close/delete
 this file; it must be locked by something else; what is that
 something else?

 The simplest thing is probably to run sysinternals' handle util:

  http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

 and use the subprocess module to parse the output.
 That will give you the pid, and you can then use, eg, psutil:

  http://code.google.com/p/psutil/

 to get the details of the process.

 TJG- Hide quoted text -

 - Show quoted text -

I've used the Handle.exe and got the following results:

--
notepad.exe pid: 3540 COMP\rajatd
C: File  (RW-)   C:\Documents and Settings\rajatd\Desktop
   10: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   44: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   7C: Section   \BaseNamedObjects\ShimSharedMemory

--
wordpad.exe pid: 2212 COMP\rajatd
   1C: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   40: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   74: Section   \BaseNamedObjects\ShimSharedMemory
   F8: Section   \BaseNamedObjects
\CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653
  170: Section   \BaseNamedObjects\RotHintTable
  184: File  (RW-)   C:\Documents and Settings\rajatd\My Documents

I've also parsed this output for the PIDS. But no where in the result
I got to know what file has been associated with a PID.

Does for this I need to use pustil?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-09 Thread Tim Golden

Rajat wrote:

I've used the Handle.exe and got the following results:

--
notepad.exe pid: 3540 COMP\rajatd
C: File  (RW-)   C:\Documents and Settings\rajatd\Desktop
   10: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   44: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   7C: Section   \BaseNamedObjects\ShimSharedMemory

--
wordpad.exe pid: 2212 COMP\rajatd
   1C: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   40: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   74: Section   \BaseNamedObjects\ShimSharedMemory
   F8: Section   \BaseNamedObjects
\CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653
  170: Section   \BaseNamedObjects\RotHintTable
  184: File  (RW-)   C:\Documents and Settings\rajatd\My Documents

I've also parsed this output for the PIDS. But no where in the result
I got to know what file has been associated with a PID.

Does for this I need to use pustil?



Well unless I'm missing your point considerably, the output tells
you all you need to know: notepad.exe (pid 3540) has some kind
of handle open on the desktop, the common controls DLL and an
area of shared memory. As has been pointed out elsewhere, notepad
doesn't hold the file open which it's editing: it opens it, reads
the contents, and closes it again. 


For demonstration purposes:

code
import os, sys
import subprocess

f = open (sys.executable)
subprocess.call ([handle, sys.executable])
f.close ()

/code


TJG
--
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-09 Thread Rajat
On Jul 9, 1:21 pm, Tim Golden m...@timgolden.me.uk wrote:
 Rajat wrote:
  I've used the Handle.exe and got the following results:

  ---­---
  notepad.exe pid: 3540 COMP\rajatd
      C: File  (RW-)   C:\Documents and Settings\rajatd\Desktop
     10: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
  Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
     44: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
  Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
     7C: Section       \BaseNamedObjects\ShimSharedMemory

  ---­---
  wordpad.exe pid: 2212 COMP\rajatd
     1C: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
  Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
     40: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
  Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
     74: Section       \BaseNamedObjects\ShimSharedMemory
     F8: Section       \BaseNamedObjects
  \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653
    170: Section       \BaseNamedObjects\RotHintTable
    184: File  (RW-)   C:\Documents and Settings\rajatd\My Documents

  I've also parsed this output for the PIDS. But no where in the result
  I got to know what file has been associated with a PID.

  Does for this I need to use pustil?

 Well unless I'm missing your point considerably, the output tells
 you all you need to know: notepad.exe (pid 3540) has some kind
 of handle open on the desktop, the common controls DLL and an
 area of shared memory. As has been pointed out elsewhere, notepad
 doesn't hold the file open which it's editing: it opens it, reads
 the contents, and closes it again.

 For demonstration purposes:

 code
 import os, sys
 import subprocess

 f = open (sys.executable)
 subprocess.call ([handle, sys.executable])
 f.close ()

 /code

 TJG- Hide quoted text -

 - Show quoted text -

The Notepad process information is fine here. However, with wordpad
the results are not much differentiating:

--
wordpad.exe pid: 2832 COMP\rajatd
   1C: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   40: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   74: Section   \BaseNamedObjects\ShimSharedMemory
   F8: Section   \BaseNamedObjects
\CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653
  170: Section   \BaseNamedObjects\RotHintTable
  184: File  (RW-)   C:\Documents and Settings\rajatd\My Documents
--
wordpad.exe pid: 844 COMP\rajatd
   1C: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   40: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   74: Section   \BaseNamedObjects\ShimSharedMemory
   F8: Section   \BaseNamedObjects
\CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653
  170: Section   \BaseNamedObjects\RotHintTable
  184: File  (RW-)   C:\Documents and Settings\rajatd\My Documents

Both the wordpad applications opened 2 totally different files kept at
different locations on the system.

So, on the basis of above results one can not say out of these 2
wordpad apps which is the right one that could be closed. The only
different thing among the two is the PIDs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-09 Thread Tim Golden

Rajat wrote:

The Notepad process information is fine here. However, with wordpad
the results are not much differentiating:

--
wordpad.exe pid: 2832 COMP\rajatd
   1C: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   40: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   74: Section   \BaseNamedObjects\ShimSharedMemory
   F8: Section   \BaseNamedObjects
\CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653
  170: Section   \BaseNamedObjects\RotHintTable
  184: File  (RW-)   C:\Documents and Settings\rajatd\My Documents
--
wordpad.exe pid: 844 COMP\rajatd
   1C: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   40: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
   74: Section   \BaseNamedObjects\ShimSharedMemory
   F8: Section   \BaseNamedObjects
\CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653
  170: Section   \BaseNamedObjects\RotHintTable
  184: File  (RW-)   C:\Documents and Settings\rajatd\My Documents

Both the wordpad applications opened 2 totally different files kept at
different locations on the system.

So, on the basis of above results one can not say out of these 2
wordpad apps which is the right one that could be closed. The only
different thing among the two is the PIDs.



Rajat: are you trying to find out which app is holding a file open?

If so -- run handle.exe *for that filename*, as my code does:

handle.exe filename

This will only show which apps are holding that file. I suspect
you're running handle.exe on its own which will show everything
which is holding handles on anything.

1) Use wordpad.exe to open c:\temp\blah.txt

2) Run handle.exe c:\temp\blah.txt

3) Observe (at least on my Win XP Sp3 machine) that *no* process
has a handle open on c:\temp\blah.txt, including wordpad, which
presumably opens the file, reads it, and closes it again.


The items you're seeing above are system-level handles which
wordpad is holding for reasons of its own, but none of them
is a user filename.

TJG
--
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-09 Thread Rajat
On Jul 9, 3:21 pm, Tim Golden m...@timgolden.me.uk wrote:
 Rajat wrote:
  The Notepad process information is fine here. However, with wordpad
  the results are not much differentiating:

  ---­---
  wordpad.exe pid: 2832 COMP\rajatd
     1C: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
  Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
     40: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
  Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
     74: Section       \BaseNamedObjects\ShimSharedMemory
     F8: Section       \BaseNamedObjects
  \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653
    170: Section       \BaseNamedObjects\RotHintTable
    184: File  (RW-)   C:\Documents and Settings\rajatd\My Documents
  ---­---
  wordpad.exe pid: 844 COMP\rajatd
     1C: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
  Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
     40: File  (RW-)   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-
  Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
     74: Section       \BaseNamedObjects\ShimSharedMemory
     F8: Section       \BaseNamedObjects
  \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653
    170: Section       \BaseNamedObjects\RotHintTable
    184: File  (RW-)   C:\Documents and Settings\rajatd\My Documents

  Both the wordpad applications opened 2 totally different files kept at
  different locations on the system.

  So, on the basis of above results one can not say out of these 2
  wordpad apps which is the right one that could be closed. The only
  different thing among the two is the PIDs.

 Rajat: are you trying to find out which app is holding a file open?

 If so -- run handle.exe *for that filename*, as my code does:

 handle.exe filename

 This will only show which apps are holding that file. I suspect
 you're running handle.exe on its own which will show everything
 which is holding handles on anything.

 1) Use wordpad.exe to open c:\temp\blah.txt

 2) Run handle.exe c:\temp\blah.txt

 3) Observe (at least on my Win XP Sp3 machine) that *no* process
 has a handle open on c:\temp\blah.txt, including wordpad, which
 presumably opens the file, reads it, and closes it again.

 The items you're seeing above are system-level handles which
 wordpad is holding for reasons of its own, but none of them
 is a user filename.

 TJG- Hide quoted text -

 - Show quoted text -

Thanks Tim for the details. Just further on this, my whole idea is to
close the wordpad / notepad application so that I can delete the file
and the directory where this file resides.

With notepad it is no more a problem. But I'm concerned about the
wordpad now.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-09 Thread Gabriel Genellina
En Thu, 09 Jul 2009 07:53:29 -0300, Rajat rajat.dud...@gmail.com  
escribió:



Thanks Tim for the details. Just further on this, my whole idea is to
close the wordpad / notepad application so that I can delete the file
and the directory where this file resides.

With notepad it is no more a problem. But I'm concerned about the
wordpad now.


I don't see Wordpad keeping the file open either.
If you can't remove the file, use handle.exe filename.txt to discover  
which process is holding it open, as Tim already explained.


--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-08 Thread Tim Golden

Rajat wrote:

On Jul 8, 4:57 am, Lawrence D'Oliveiro l...@geek-
central.gen.new_zealand wrote:

In message mailman.2795.1246997268.8015.python-l...@python.org, Christian

Heimes wrote:

By the way most operating systems don't lock a file when it's opened for
reading or writing or even executed.

The general conclusion seems to be that mandatory locking is more trouble
than it's worth.


My OS is a windows XP sp3. All I'm trying to achieve is to close an
application ( which could be a notepad, a wordpad or some other text
editor) that have opened my file. Once the file is closed I can easily
delete that file.

I guess, if file is opened by one of that application, the file must
be locked and so is the reason I cannot delete the file.



I assume that your real requirement is: I can't open/close/delete
this file; it must be locked by something else; what is that
something else?

The simplest thing is probably to run sysinternals' handle util:

 http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

and use the subprocess module to parse the output.
That will give you the pid, and you can then use, eg, psutil:

 http://code.google.com/p/psutil/

to get the details of the process.

TJG
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Check file is locked?

2009-07-08 Thread Dave Angel

Rajat wrote:

On Jul 8, 4:57 am, Lawrence D'Oliveiro l...@geek-
central.gen.new_zealand wrote:
  

In message mailman.2795.1246997268.8015.python-l...@python.org, Christian

Heimes wrote:


By the way most operating systems don't lock a file when it's opened for
reading or writing or even executed.
  

The general conclusion seems to be that mandatory locking is more trouble
than it's worth.



My OS is a windows XP sp3. All I'm trying to achieve is to close an
application ( which could be a notepad, a wordpad or some other text
editor) that have opened my file. Once the file is closed I can easily
delete that file.

I guess, if file is opened by one of that application, the file must
be locked and so is the reason I cannot delete the file.

Please suggest if this is fine.


  
Neither Wordpad nor Notepad lock the files they have open, at least in 
my experience.  Specifically, I just tried it in XP Sp3.   I created a 
file, opened the file in one of the apps, then deleted the file before 
closing the app.


Word for Windows, on the other hand, has locked files for me in the 
past.  When I tried that one just now (on a text file, because that was 
easy), results were rather confusing.  It let me delete the file, but 
then when I tried to save it, it gave me a permissions error.  My 
experience with Winword has been that it's in a world of its own, best 
not messed with.  And this latest version (Office 2007 or somesuch) is 
so modern that it has removed the Help-About menu to make sure I can't 
even tell what version it is.  Next time I rebuild my system, I think 
I'll go back to Office 97.



But perhaps your question is intended to cover the general case.  You 
can get a program from sysinternals called   handle, which will list all 
the files (and other handles) opened by all the processes in the 
system.  You may be able to run that (with subprocess, or just from the 
commandline), and filter its output for the information you want.



DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-08 Thread Steven D'Aprano
On Wed, 08 Jul 2009 00:06:11 -0700, Dennis Lee Bieber wrote:

   Also, some applications may still have the file open, but Windows
 allows one to make copies of that file...


Not always though... some applications open files for exclusive read 
access.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-08 Thread Nobody
On Tue, 07 Jul 2009 21:31:12 -0700, Rajat wrote:

  By the way most operating systems don't lock a file when it's opened for
  reading or writing or even executed.

 The general conclusion seems to be that mandatory locking is more trouble
 than it's worth.
 
 My OS is a windows XP sp3. All I'm trying to achieve is to close an
 application ( which could be a notepad, a wordpad or some other text
 editor) that have opened my file. Once the file is closed I can easily
 delete that file.
 
 I guess, if file is opened by one of that application, the file must
 be locked and so is the reason I cannot delete the file.

Windows doesn't allow you to delete (or rename) open files. It's up to the
application as to whether it keeps the file open or closes it once it has
been read.

 Please suggest if this is fine.

If you can't delete the file, then you can't delete it. You shouldn't be
trying to kill off applications so that you can delete the file. Just do
what other Windows programs (including those which are part of Windows)
do: display a dialog telling the user to close the application then try
again.

-- 
http://mail.python.org/mailman/listinfo/python-list


Check file is locked?

2009-07-07 Thread dudeja . rajat
How to check if a particular file is locked by some application? (i.e. the
file is opened by some application)?

-- 
Regrads,
Rajat
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-07 Thread Christian Heimes
dudeja.ra...@gmail.com wrote:
 How to check if a particular file is locked by some application? (i.e. the
 file is opened by some application)?

It depends on your operating system. By the way most operating systems
don't lock a file when it's opened for reading or writing or even executed.

Christian

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-07 Thread Lawrence D'Oliveiro
In message mailman.2795.1246997268.8015.python-l...@python.org, Christian 
Heimes wrote:

 By the way most operating systems don't lock a file when it's opened for
 reading or writing or even executed.

The general conclusion seems to be that mandatory locking is more trouble 
than it's worth.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Check file is locked?

2009-07-07 Thread Rajat
On Jul 8, 4:57 am, Lawrence D'Oliveiro l...@geek-
central.gen.new_zealand wrote:
 In message mailman.2795.1246997268.8015.python-l...@python.org, Christian

 Heimes wrote:
  By the way most operating systems don't lock a file when it's opened for
  reading or writing or even executed.

 The general conclusion seems to be that mandatory locking is more trouble
 than it's worth.

My OS is a windows XP sp3. All I'm trying to achieve is to close an
application ( which could be a notepad, a wordpad or some other text
editor) that have opened my file. Once the file is closed I can easily
delete that file.

I guess, if file is opened by one of that application, the file must
be locked and so is the reason I cannot delete the file.

Please suggest if this is fine.

-- 
http://mail.python.org/mailman/listinfo/python-list