copy locked files

Jay Loden python at jayloden.com
Mon Jun 18 19:45:25 CEST 2007



Adam Pletcher wrote:
> Do you mean files marked in-use by the OS, like DLLs used by an open
> application?
> 
> There shouldn't be anything preventing you from copying in-use files, or
> even read-only files if that's what you meant:
> 
>   import shutil
>   shutil.copy('C:\\my_application\\test.dll',
> 'C:\\new_folder\\test.dll')
> 
> Although you can't move or delete an in-use file, AFAIK.

I had to deal with a similar problem using the Win32 API in C recently. You can't move or delete an in-use file, as Adam noted. However, you *can* rename a file (see http://www.nntp.perl.org/group/perl.makemaker/2006/01/msg2437.html as a nice summation of the problem), and MoveFileEx() in the Win32 API has a flag that will delete a file on the next reboot. 

As far as copying an in-use file, there are two possibilities I can think of. One would be to try disconnecting the lock using the openfiles command (this may not exist on all versions of Windows, but it is there on my WinXP box). The other would be to use Locked Files Wizard (used to be called CopyLock) http://noeld.com/programs.asp?cat=misc 

It comes with a command line version of the tool that you can call from your Python script as necessary. Neither is an ideal solution but I can't find any other suggestions out there on dealing with this from Python. 

HTH, 

-Jay



More information about the Python-list mailing list